将 url 与模式匹配
我正在打开一个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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将
find()
和 if/else 分开。您只需在 for 循环中调用find()
一次。Javadoc 说:
Matcher.find() 尝试查找输入序列中与模式匹配的下一个子序列。
Next 非常重要,这意味着每次调用该方法时都会跳到下一个可能的匹配。Try seperating the
find()
and your if/else. You need to call thefind()
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.