Noam
Noam

Reputation: 3145

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)://[a-z.]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?

Upvotes: 0

Views: 2115

Answers (3)

Kul Bhushan Prasad
Kul Bhushan Prasad

Reputation: 879

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());
}

Upvotes: 0

Michael Besteck
Michael Besteck

Reputation: 2423

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)");
        }
    }

Upvotes: 1

Mike Brant
Mike Brant

Reputation: 71384

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

Upvotes: 1

Related Questions