0

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

5
  • So you want to check if a string starts with either http or www, correct? Commented Aug 13, 2014 at 10:49
  • Your second point and your last test case is contradicting. Commented Aug 13, 2014 at 10:56
  • @Unihedron which one I dont understand. Commented Aug 13, 2014 at 11:02
  • @Antony "if string didnot start with "http", string have to start with "www"". In www, it did not start with http, but did start with www (itself)! Commented Aug 13, 2014 at 11:02
  • Now that you've edited to clarify, I rolled-back my solution. My old answer did work what you wanted, after all. Commented Aug 13, 2014 at 11:07

4 Answers 4

1

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.
Sign up to request clarification or add additional context in comments.

2 Comments

no I have said before, if it starts with "http" after it "www" can come. But second output is false, it should be true. Wait I will add some input
@Antony take a look at my answer. It should cover your examples.
1

Effectively, your String should start with either http or www. You don't care if http is followed by www. All you care about is your string should always start with *http8 or www and nothing else. So,

public static void main(String[] args) {
    String s1 = "http://www.google.com";
    String s2 = "www.google.com";
    String s3 = "sdfwww.google.com";
    System.out.println(s1.matches("^(http|www).*"));
    System.out.println(s2.matches("^(http|www).*"));
    System.out.println(s3.matches("^(http|www).*"));

}

O/P ::

true
true
false

Comments

1

Use a simple regex match:

boolean matches = myString.matches("^(?:http|www).*");
// The .* is for Java implementation

Here is a regex demo!

4 Comments

your regex means one of the "http" or "www" can exist or both of them. However, there is no have to relation. IF I downvote, I will use it for you
@Antony - Simply put, your question boils down to - does my regex start with either http or www. right?.
Why the ?: non-capturing group?
@TheLostMind The Java implementation incurs processing time writing each backreference location and match for each capturing group match. Hence for Java Regexes, I use capturing groups sparingly. YMMV due to JIT compiler optimizations.
0

Try this Demo,

http://regex101.com/r/dW7jJ0/3

Regex is

^(httpwww|http|www)

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.