简体   繁体   中英

Differences between Jakarta Regexp and Java 6 java.util.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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM