将 url 与模式匹配

发布于 2024-12-03 20:38:55 字数 1789 浏览 0 评论 0原文

我正在打开一个filters.txt 文件。下面是该文件:

http://www.somehost.com/.*/releases, RE,TO

我正在将文本文件中的第一个条目与代码中的硬编码 URL 进行比较。文本文件中以模式(firstentry)开头的任何 URL 都应该执行此操作,特别是 if 循环。这里这个网址http://www.somehost.com/news/releases/2011/09/07/somehost-and-life-care-networks-launch-3g-mobile-health-project-help-patien< /code> 仅源自此模式 url http://www.somehost.com/.*/releases。但它仍然不符合这个模式。有什么建议吗?为什么会发生这种情况?

BufferedReader readbuffer = null;
            try {
                readbuffer = new BufferedReader(new FileReader("filters.txt"));
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            String strRead;



        try {
            while ((strRead=readbuffer.readLine())!=null){
                String splitarray[] = strRead.split(",");
                String firstentry = splitarray[0];
                String secondentry = splitarray[1];
                String thirdentry = splitarray[2];



                Pattern p = Pattern.compile("^" +firstentry);
                Matcher m = p.matcher("http://www.somehost.com/news/releases/2011/09/07/somehost-and-life-care-networks-launch-3g-mobile-health-project-help-patien");

                if (m.find() && thirdentry.startsWith("LO")) {
                  //Do whatever

                    System.out.println("First Loop");

                }

                else if(m.find() && thirdentry.startsWith("TO"))
                {
                    System.out.println("Second Loop");
                }

                }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 

I am opening a filters.txt file. Below is the file:

http://www.somehost.com/.*/releases, RE,TO

I am comparing the first entry in the text file with the hard coded url in my code. Any URL that starts with the pattern(firstentry) in the text file should do this in particular if loop. Here this url http://www.somehost.com/news/releases/2011/09/07/somehost-and-life-care-networks-launch-3g-mobile-health-project-help-patien is originated from this pattern url only http://www.somehost.com/.*/releases. But still it is not matching this pattern. Any suggestions why is it happening?

BufferedReader readbuffer = null;
            try {
                readbuffer = new BufferedReader(new FileReader("filters.txt"));
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            String strRead;



        try {
            while ((strRead=readbuffer.readLine())!=null){
                String splitarray[] = strRead.split(",");
                String firstentry = splitarray[0];
                String secondentry = splitarray[1];
                String thirdentry = splitarray[2];



                Pattern p = Pattern.compile("^" +firstentry);
                Matcher m = p.matcher("http://www.somehost.com/news/releases/2011/09/07/somehost-and-life-care-networks-launch-3g-mobile-health-project-help-patien");

                if (m.find() && thirdentry.startsWith("LO")) {
                  //Do whatever

                    System.out.println("First Loop");

                }

                else if(m.find() && thirdentry.startsWith("TO"))
                {
                    System.out.println("Second Loop");
                }

                }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽清梦 2024-12-10 20:38:55

尝试将 find() 和 if/else 分开。您只需在 for 循环中调用 find() 一次。

Javadoc 说:
Matcher.find() 尝试查找输入序列中与模式匹配的下一个子序列。 Next 非常重要,这意味着每次调用该方法时都会跳到下一个可能的匹配。

boolean found = m.find();
if (found && thirdentry.startsWith("LO")) {
//Do whatever
  System.out.println("First Loop");
}
else if(found && thirdentry.startsWith("TO"))
{
  System.out.println("Second Loop");
}

Try seperating the find() and your if/else. You need to call the find() only once in your for loop.

Javadoc says:
Matcher.find() Attempts to find the next subsequence of the input sequence that matches the pattern. Next is quite important, it means it skips to the next possible match every time you call the method.

boolean found = m.find();
if (found && thirdentry.startsWith("LO")) {
//Do whatever
  System.out.println("First Loop");
}
else if(found && thirdentry.startsWith("TO"))
{
  System.out.println("Second Loop");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文