Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Regex not matching?

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?

like image 616
Isaac Avatar asked Aug 18 '11 06:08

Isaac


2 Answers

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
like image 83
Prince John Wesley Avatar answered Oct 12 '22 18:10

Prince John Wesley


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

like image 35
Dragon8 Avatar answered Oct 12 '22 18:10

Dragon8