简体   繁体   中英

Java - Need some help on a Regex pattern for matching both http and https

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.

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

用这个:

Pattern.compile("^(https?://(?![^.]*\\.google\\.com)[^/]*)");

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

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

如果http://www.google.comhttps://www.google.com与我当前的网址匹配,我只希望它返回true。

Pattern.compile("(?i)^https?://www\\.google\\.com\\z");
    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM