Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between Jakarta Regexp and Java 6 java.util.regex

Tags:

java

regex

I am in the process of migrating from Jakarta Regexp to the standard Java 6 regular expressions package java.util.regex. I noticed the following difference when not specifying the beginning ^ and end $ in a regexp: Jakarta Regexp returns true when the regexp matches part of the string, while the Java 6 java.util.regex package does not:

String regexp = "\\d";
String value = "abc1abc";

Pattern pattern = Pattern.compile(regexp);
Matcher matcher = pattern.matcher(value);
result = matcher.matches(); // returns false

Returns false whereas:

RE re = new RE(regexp);
re.match(value); // returns true

Returns true.

What is the reason behind this? I've thought about greedy/lazy matching but that doesn't seem to be relevant in the case of JDK 6 not matching.

Are there any other differences that I should be aware of?

like image 398
vahidg Avatar asked Jul 05 '11 12:07

vahidg


2 Answers

The java.util.regex.Matcher.matches() method will try to match the complete input string against your regular expression which will be false.

If you want to search for the pattern in the input string, you'll need to use java.util.regex.Matcher.find() method instead:

 result = matcher.find(); // returns true
like image 161
wjans Avatar answered Sep 26 '22 00:09

wjans


Use find() instead of matches(). It functions exactly as you are expecting.

like image 30
AlexR Avatar answered Sep 25 '22 00:09

AlexR