1

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.

Whnunlife
  • 179
  • 1
  • 3
  • 13
  • You need to post the code where it's returning true for all of the urls. In particular your pattern should not match any url with https either so something is wrong when you test to see if it matches – BoredAndroidDeveloper Mar 09 '12 at 20:54
  • 2
    Not quite the answer you wanted, but why not use a proper URL parser instead of a homebrew regex? http://docs.oracle.com/javase/1.4.2/docs/api/java/net/URL.html – Freiheit Mar 09 '12 at 20:57
  • I'll look into the URL parser as well, thank you! – Whnunlife Mar 09 '12 at 21:14

6 Answers6

1

How about just .contains("//google.com")? Or if "google.com" is at position seven or eight?

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
1

Use this:

Pattern.compile("^(https?://(?![^.]*\\.google\\.com)[^/]*)");
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Thank you! That worked. I did have to change it slightly to:final Pattern p = Pattern.compile("^(https?://(?!.*.google.com)[/]*)"); – Whnunlife Mar 09 '12 at 21:00
1

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

Community
  • 1
  • 1
Adam
  • 35,919
  • 9
  • 100
  • 137
0

final Pattern p = Pattern.compile( "(https?:(?!.*google.com).*)" );

Mārtiņš Briedis
  • 17,396
  • 5
  • 54
  • 76
  • This one is working also: Had to make a slight change, notice the end i added the slash, their were some other url's that were matching true, because it had the domain name that I'm searching for in the tracking query tag. final Pattern p = Pattern.compile( "(https?:(?!.*google.com/).*)" ); – Whnunlife Mar 09 '12 at 21:13
0

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");
Qtax
  • 33,241
  • 9
  • 83
  • 121
0
    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
dule
  • 17,798
  • 4
  • 39
  • 38