开发者

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";
Strin开发者_JAVA百科g 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜