任何以模式开头的 url.. 执行此操作
我想将 url A (http://www.somehost.com/citizenship
) 与模式 url B (http://www.somehost.com/*
) 进行比较),如果 URL A 以模式 url B 开头,则执行此操作。由于 URL A 源自 URL B。所以任何以此模式开头的 url.. 只需在 if 循环中执行此操作...任何建议将不胜感激...!!
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.somehost.com/citizenship");
//Any url that starts with this pattern then do whatever you want in the if loop... How can we implement this??
Pattern p = Pattern.compile("http://www.somehost.com/*");
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();
}
I want to compare url A (http://www.somehost.com/citizenship
) with the pattern url B (http://www.somehost.com/*
) and if that URL A starts with the pattern url B then do this thing.. As URL A is originated from the URL B.. So any url that starts with this pattern.. just do this thing in the if loop... Any suggestions will be appreciated...!!
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.somehost.com/citizenship");
//Any url that starts with this pattern then do whatever you want in the if loop... How can we implement this??
Pattern p = Pattern.compile("http://www.somehost.com/*");
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
String.startsWith(String)
?您可以像这样调用它:
url1.toString().startsWith("http://www.somehost.com/")
。或者我错过了你到底想要实现的目标吗?
无论如何,这是要构建的正则表达式:
http://www\.somehost\.com/.*
您需要学习正则表达式语法,其中“.”表示任何字符,因此您需要在“www”和“somehost”之间对其进行转义。
在 Java 代码中,您还需要转义退格键,因此它变为:
Pattern.compile("http://www\\.somehost\\.com/.*");
What's wrong with
String.startsWith(String)
?You could invoke it like this:
url1.toString().startsWith("http://www.somehost.com/")
.Or did I miss what you're exactly trying to achieve?
Anyways, this is the regular expression to build:
http://www\.somehost\.com/.*
You need to learn regular expression syntax, in which "." mean any character so you need to escape it in between "www" and "somehost".
In Java code, you need to escape backspaces as well hence it becomes:
Pattern.compile("http://www\\.somehost\\.com/.*");
我相信这应该可以解决问题:
I believe this should do the trick: