简体   繁体   中英

How to find a url pattern in java using regex

I want to find out if a given string (that represent a url) is from the same sub domain. For example, http://www.myDomain.com/someThing with the combination of myDomain.com will return true. So will the following:

http://myDomain.com ; http://www.domain.myDomain.com ;

But the next (illeagal) url will not - 'http://.myDomain.com' (note the dot before myDomain)

Basically, I need a regex that represent whatever before myDomain.com - which in general needs to be (http|https)://[az.] myDomain - which mean that just before myDomain.com there might be letters followed by dot (0 or more times) - but if there are no letters, there shouldn't be dot as well.

Does anyone know how to assemble that regex?

http(s)?://([a-z]+\.)*myDomain\.com

It can be done with a combination of the URL class and a regular expression:

    String url = "myDomain.com";
    String[] urlTest = {
        "http://www.myDomain.com/someThing",
        "http://myDomain.com",
        "http://www.domain.myDomain.com",
        "http://.myDomain.com",
        "http://example.com"

    };
    for (String urlx : urlTest) {
        System.out.print(urlx + "\t");
        try {
            URL u = new URL(urlx);
            String host = u.getHost();
            System.out.print("HOST=" + host + "\t");
            Matcher m = Pattern.compile("(.+\\.)?myDomain\\.com").matcher(host);
            System.out.println(m.matches());

        } catch (MalformedURLException ex) {
            System.out.println("false (no valid url)");
        }
    }

Putting example here:

Pattern aPattern = Pattern.compile("https://example.com[^\"<$\n \\[\\])]+", 
Pattern.MULTILINE);
            Matcher aMatcher = aPattern.matcher(Big String);
while (aMatcher.find()) {
logger.info(aMatcher.group());
}

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