0

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?

1

3 Answers 3

1
http(s)?://([a-z]+\.)*myDomain\.com
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.