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.
source share