Reputation: 189
I'm struggling with getting my regex pattern to match. Here are my requirements...
Match the following domain ex, (google.com) with both http and https.
I have an array list of various URL's....
http://stackoverflow.com/questions/ask
https://ask.com/search
http://google.com/images
https://google.com/images
This is my Pattern:
final Pattern p = Pattern.compile( "(http:(?!.*google.com).*)" );
However, it's currently returning true for all my url's.
Again, I only want it to return true if http://www.google.com or https://www.google.com matches my current url.
Upvotes: 1
Views: 1736
Reputation: 18168
String[] urls = new String[] {
"http://stackoverflow.com/questions/ask",
"https://ask.com/search",
"http://google.com/images",
"https://google.com/images",
"http://www.google.com"
};
final Pattern p = Pattern.compile( "https?://.*?google\\.com.*?" );
for (String url : urls) {
Matcher m = p.matcher(url);
System.out.println(m.matches());
}
Output is:
false
false
true
true
true
Upvotes: 0
Reputation: 36723
How about java.net.URI or URL classes...
try {
URI url = new URI("https://www.google.com/foo?test=horse");
System.out.println(url.getScheme()); // https
System.out.println(url.getHost()); // www.google.com
System.out.println(url.getPath()); // /foo
System.out.println(url.getQuery()); // test=horse
} catch (URISyntaxException e) {
e.printStackTrace();
}
Edit: I used URI because I remember hearing somewhere URL had side effects. Just checked it does, the hashCode() method does DNS lookups. Therefore stick to URI if you just want to re-use the URL parsing functionality... See this question
Upvotes: 1
Reputation: 785611
Use this:
Pattern.compile("^(https?://(?![^.]*\\.google\\.com)[^/]*)");
Upvotes: 1
Reputation: 33918
I only want it to return true if http://www.google.com or https://www.google.com matches my current url.
Pattern.compile("(?i)^https?://www\\.google\\.com\\z");
Upvotes: 0
Reputation: 160261
How about just .contains("//google.com")
? Or if "google.com"
is at position seven or eight?
Upvotes: 1
Reputation: 17762
final Pattern p = Pattern.compile( "(https?:(?!.*google.com).*)" );
Upvotes: 0