Reputation: 24174
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();
}
Upvotes: 1
Views: 1811
Reputation: 70234
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/.*");
Upvotes: 1
Reputation: 4907
I believe this should do the trick:
private final static String regexPattern = "http://www.somehost.com/[0-9a-zA-Z\\-]*/wireless-reach(/[0-9a-zA-Z\\-]*)*";
String pattern = "http://www.somehost.com/citizen-ship/wireless-reach/fil1";
if(pattern.matches(regex))
System.out.println("Matched!");
Upvotes: 0