5

I have this code, but it does not seem to be working.

Pattern pattern=Pattern.compile("IMGURSESSION=([0-9a-zA-Z]*);");
Matcher matcher=pattern.matcher("IMGURSESSION=blahblah; path=/; domain=.imgur.com");
System.out.println(matcher.matches());

Would anyone know why?

2
  • There is nothing wrong with the regex itself. It worked when I tested it with RegexBuddy. Commented Aug 18, 2011 at 6:08
  • 1
    I know that. I know regex enough to know it will work. And RegexBuddy--40 dollars! Insane! I just stick to gskinner.com/RegExr Commented Aug 18, 2011 at 6:20

3 Answers 3

8

Matcher#matches() method attempts to match the entire input sequence against the pattern.

Pattern.compile("IMGURSESSION=([0-9a-zA-Z]*);.*$"); //true
Pattern.compile("IMGURSESSION=([0-9a-zA-Z]*);"); //false
Sign up to request clarification or add additional context in comments.

Comments

2

the matches Method match against the entire input string.

if you will match only a subsequence you can use the find() method.

the 3 different ways to match with a matcher are explained in the java docs: http://download.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html

Comments

1

Assuming your aim is to extract the IMGURSESSION:

import java.util.regex.*;

Pattern pattern = Pattern.compile("IMGURSESSION=([0-9a-zA-Z]*);.*");
Matcher matcher = pattern.matcher("IMGURSESSION=blahblah; path=/; domain=.imgur.com");
if (matcher.find()) {
    System.out.println(matcher.group(1));
}

Just make sure you put in a match all pattern at the end to satisfy the "matcher" semantics.

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.