如何匹配文本文件中模式列表中的 url?
我有一个包含以下形式的元 URL 的文本文件:
http://www.xyz.com/.*services/
http://www.xyz.com/.*/wireless
我想将该文件中的所有模式与我的 URL 进行比较,如果找到匹配则执行操作。这个匹配过程对我来说很难理解。
假设 splitarray[0] 包含文本文件的第一行:
String url = page.getWebURL().getURL();
URL url1 = new URL(url);
我们如何将 url1 与 splitarray[0] 进行比较?
更新
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];
//String fourthentry = splitarray[3];
//String fifthentry = splitarray[4];
System.out.println(firstentry + " " + secondentry+ " " +thirdentry);
URL url1 = new URL("http://www.xyz.com/ship/reach/news-and");
Pattern p = Pattern.compile("http://www.xyz.com/.*/reach");
Matcher m = p.matcher(url1.toString());
if (m.matches()) {
//Do whatever
System.out.println("Yes Done");
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
匹配工作正常...但是,如果我想要任何以 splitarray[0] 中给出的模式开头的 url,那么就这样做...我们如何实现这个...如上述情况不匹配,但此 url http://www.xyz.com/ship/w
仅来自此模式 http://www.xyz.com/.* /reach
所以任何以此模式开头的网址..只需执行此操作if 循环中的事情...任何建议将不胜感激...!!
I have a text file that contains meta-urls in the following form:
http://www.xyz.com/.*services/
http://www.xyz.com/.*/wireless
I want to compare all the patterns from that file with my URL, and execute an action if I find a match. This matching process is hard to understand for me.
Assuming splitarray[0] contains the first line of text file:
String url = page.getWebURL().getURL();
URL url1 = new URL(url);
how can we compare url1 with splitarray[0]?
UPDATED
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];
//String fourthentry = splitarray[3];
//String fifthentry = splitarray[4];
System.out.println(firstentry + " " + secondentry+ " " +thirdentry);
URL url1 = new URL("http://www.xyz.com/ship/reach/news-and");
Pattern p = Pattern.compile("http://www.xyz.com/.*/reach");
Matcher m = p.matcher(url1.toString());
if (m.matches()) {
//Do whatever
System.out.println("Yes Done");
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Matching is working fine... But if I want that any url which start with the pattern giving in the splitarray[0] then do this... how we can implement this... As in the above case it is not matching but this url http://www.xyz.com/ship/w
is from this pattern only http://www.xyz.com/.*/reach
So any url that starts with this pattern.. just do this thing in the if loop... Any suggestions will be appreciated...!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你在这里错过了一步。您首先需要将 URL 转换为正则表达式,或者设计一种使用这些 URL 的方法,然后才能将 URL url1 与这些模式进行比较。
根据您所展示的模式,我假设您正在为 xyz 解决方案设计软件,例如他们的路由器。因此,您的 URL 可能属于简单的模式样式,例如
http://www.xyz.com/正则表达式-此处
You are missing a step here. You first need to translate your URLs to a regular expression, or design a method to use those URLs, then only can you compare your URL url1 to those patterns.
Based on the patterns you have shown, I assume you are designing software for a xyz solution, like their routers. Therefore, your URLs probably fall in a simple pattern style, like
http://www.xyz.com/regular-expression-here
我对正则表达式的来源感到困惑。文本文件?无论如何,您都很难将
url1
与任何正则表达式进行比较,因为它是一个URL
对象,而正则表达式会比较字符串。因此,您需要坚持使用String url
。试试这个:
m.matches()
方法检查您提供的整个字符串是否与模式匹配,这可能就是您想要的。如果您需要检查字符串的一部分是否匹配,请改用m.find()
。更新
由于您只想匹配字符串开头的模式,因此您需要使用
m.find()
来代替。特殊字符^
仅匹配字符串的开头,因此将其添加到正则表达式的前面,例如:等。
I'm confused as to where the regexes are coming from. The text file? In any case, you'll have a hard time comparing
url1
to any regexes because it's aURL
object, and regex compares strings. So you'll want to stick with yourString url
instead.Try this:
The
m.matches()
method checks whether the entire String you provide matches the pattern, which is probably what you want here. If you need to check whether part of your String matches, usem.find()
instead.Update
Since you're only looking to match the pattern at the beginning of the String, you'll want to use
m.find()
instead. The special character^
only matches at the beginning of a String, so add that to the front of your regex, e.g.:etc.