How to avoid implicit "^" and "$" in Java regular expression matching?

I struggled with doing relatively simple regular expressions in Java 1.4.2. I’m much more comfortable using Perl. Here's what happens:

I am trying to combine / ^ <foo> / from "<foo> <bar>"

I'm trying to:

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

And I get "false"

I used to say:

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

which really returns true.

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

"The String method further optimizes the search criteria by placing the invisible element ^ before the pattern and $ 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 value. However, I do not want this template. Termination. * Should not be necessary.

Then I discovered the Matcher.useAnchoringBounds (boolean) method. I thought this clearly indicates that he is not using bindings. This is not true. I tried to release

 myMatcher.reset(); 

if I needed to clear it after the attribute was disabled. Bad luck. Subsequently, calling .matches () still returns false.

What did I miss?

Edit: Well, that was easy, thanks.

+4
source share
3 answers

Use the Matcher find method (instead of matches )

+11
source

Matcher.useAnchoringBounds() was added in JDK1.5, so if you are using 1.4, I'm not sure if this will help you even if it works (pay attention to @since 1.5 in Javadocs).

The Javadocs for Matcher also states that the match() method :

Attempt to match target area with pattern.

(my emphasis)

This explains why you got .matches() == true when you changed the template to the end with .* .

To map yourself to a region starting from the very beginning, but not necessarily requiring coordination of the entire area, use either find() or lookingAt() .

+3
source

If you study "matching," what part of the input string do you expect to find?

In other words,

 Matcher myMatcher= myPattern.matcher("<foo><bar>"); if (myMatcher.matches()) { System.out.println(myMatcher.group(0)); } 

& hellip; should print what?

If you expect it to print only " <foo> ", use the find() method on Matcher instead of matches() . If you really want to find matches when input starts with " <foo> ", you need to explicitly specify this with '^' .

If you expect it to match " <foo><bar> ", you need to include the final " .* ".

+3
source

All Articles