2

I am trying to find the regex of the following link http://anythingbutrbitrary.blogspot.in/search?updated-max=2014-02-10T11:58:00-08:00&max-results=1.

My code with regex http://anythingbutrbitrary.blogspot.in/search?updated-max=\\d{4}-\\d{2}-d{2}T\\d{2}:\\d{2}:\\d{2}-\\d{2}-\\d{2}&max-results=\\d{1} returning false. What is wrong with the regex? My java code is as follows:

String regex= "http://anythingbutrbitrary.blogspot.in/search?updated-max=\\d{4}-\\d{2}-d{2}T\\d{2}:\\d{2}:\\d{2}-\\d{2}-\\d{2}&max-results=\\d{1}";
Pattern p=Pattern.compile(Pattern.quote(regex));
Matcher m = p.matcher("http://anythingbutrbitrary.blogspot.in/search?updated-max=2014-02-10T11:58:00-08:00&max-results=1");
System.out.println(m.lookingAt());

Please help. Thanks

3 Answers 3

2

Couple of mistakes in your regex.

  1. You are calling Pattern.quote which will make your regex literal.
  2. Meta characters like dot and ? need to be escaped to \\. and \\?

EDIT: After looking at your regex I found that is also wrong. Use this regex:

String regex="http://anythingbutrbitrary\\.blogspot\\.in/search\\?updated-max=\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}-\\d{2}:\\d{2}&max-results=\\d";
Sign up to request clarification or add additional context in comments.

2 Comments

You also need to escape the backslash \/
No that is not needed in Java.
1

Couple of things:

  • escape the ? with a backslash, otherwise it means "1 or 0 of previous term", so you're losing a character, which is why it's not matching (technically you should escape the dots too, but hardly worth it)
  • use String.matches() instead - it's easier to use

Try this:

String regex = "http://anythingbutrbitrary.blogspot.in/search\\?updated-max=\\d{4}-\\d{2}-d{2}T\\d{2}:\\d{2}:\\d{2}-\\d{2}-\\d{2}&max-results=\\d";
if (urlStr.matches(regex))

Comments

1

Pattern.quote() may not do what you want it to do...

From the online API:

This method produces a String that can be used to create a Pattern that would match the string s as if it were a literal pattern.

Metacharacters or escape sequences in the input sequence will be given no special meaning.

In addition, the lookingAt() javadoc says (emphasis mine):

Returns: true if, and only if, a prefix of the input sequence matches this matcher's pattern

Few minor mistakes in your regex, surrounded by two stars:

http://anythingbutrbitrary**.**blogspot**.**in/search**?**updated-max=\\d{4}-\\d{2}-**d{2}**T\\d{2}:\\d{2}:\\d{2}-\\d{2}**-**\\d{2}&max-results=\\d{1}
  1. Should escape period (otherwise you'll match any character. Doesn't really matter for this specific case, though)
  2. Same as 1.
  3. Escape the question mark (it means "one or none of the preceding character", which certainly won't produce correct results)
  4. Forgot to escape d (should be \\d)
  5. dash instead of colon

You should make your pattern code by doing Pattern.compile(regex);. Right now, you're making a pattern that matches regex as is, with escapes and metacharacters ignored. In addition, I think you might want m.matches() rather than m.lookingAt()...

1 Comment

I thought that's what I said? Was I not clear enough?

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.