Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I avoid the implicit "^" and "$" in Java regular expression matching?

Tags:

java

regex

I've been struggling with doing some relatively straightforward regular expression matching in Java 1.4.2. I'm much more comfortable with the Perl way of doing things. Here's what's going on:

I am attempting to match /^<foo>/ from "<foo><bar>"

I try:

Pattern myPattern= Pattern.compile("^<foo>");
Matcher myMatcher= myPattern.matcher("<foo><bar>");
System.out.println(myMatcher.matches());

And I get "false"

I am used to saying:

print "<foo><bar>" =~ /^<foo>/;

which does indeed return true.

After much searching and experimentation, I discovered this which said:

"The String method further optimizes its search criteria by placing an invisible ^ before the pattern and a $ after it."

When I tried:

Pattern myPattern= Pattern.compile("^<foo>.*");
Matcher myMatcher= myPattern.matcher("<foo><bar>");
System.out.println(myMatcher.matches());

then it returns the expected true. I do not want that pattern though. The terminating .* should not be necessary.

Then I discovered the Matcher.useAnchoringBounds(boolean) method. I thought that expressly telling it to not use the anchoring bounds would work. It did not. I tried issuing a

myMatcher.reset();

in case I needed to flush it after turning the attribute off. No luck. Subsequently calling .matches() still returns false.

What have I overlooked?

Edit: Well, that was easy, thanks.

like image 902
Omniwombat Avatar asked Dec 04 '22 16:12

Omniwombat


1 Answers

Use the Matcher find method (instead of the matches method)

like image 92
jdigital Avatar answered Dec 26 '22 12:12

jdigital