How can I implement this condition with regex?

advertisements

With regex in java, how do one put condition, i.e. if-then-else? Clearly say, I want to put condition if "http" exist, "www" CAN exist or if "http" doesnot exist "www" HAVE TO exist in the string. How can I implement that condition with regex?

What I done

      ((http){0,1}|(www){0,1})

But this is not work

example input :

  • if string starts with "http" after it, "www" can come.

    ex: "httpwww < sometext >" --->should be matched

    ex: "http < sometext >" --->should be matched

  • if string didnot start with "http", string have to start with "www"

    ex: "www < sometext >" --->should be matched

    ex: "< sometext >" ----> should not be matched by regex because it did not started with nor http and www


A simplistic solution for that would be to use "look around"s, as specified in comments.

You can also use an actual URL object to do that if, as I imagine, you're working with actual URLs.

For instance:

String[] input = { "http://www.google.com", "http://foo.com", "www.foo.com" };
Pattern p = Pattern.compile("(?<=http).*(?=www)");
for (String s : input) {
    Matcher m = p.matcher(s);
    System.out.printf("Found in %s? %b%n", s, m.find());
    try {
        URL u = new URL(s);

        System.out.printf("Authority starts with www and protocol is http for %s? %b%n", s,
                u.getAuthority().startsWith("www") && u.getProtocol().equals("http"));
    }
    catch (MalformedURLException mue) {
        System.out.printf("%s is not interpreted as well-formed URL.%n", s);
    }
}

Output

Found in http://www.google.com? true
Authority starts with www and protocol is http for http://www.google.com? true
Found in http://foo.com? false
Authority starts with www and protocol is http for http://foo.com? false
Found in www.foo.com? false
www.foo.com is not interpreted as well-formed URL.