matches requires the whole string
String.matches tests whether the entire input matches the pattern, unlike a search for any matching substring. Use find or add appropriate wildcards when checking containment.
String.splitalso treats its argument as a regex, so splitting on a pipe character needsPattern.quote. Got a different fix? Post it in the comments.String.matches requires the pattern to cover the full input, so a partial occurrence returns false. For a containment check, compile a Pattern and call find, or add wildcards at both ends.
matches is anchored to the entire string, which surprises people expecting a substring search. A containment test should use find on a compiled Pattern, or wildcards placed at both ends of the regex.
Appreciate the write-up on "matches requires the whole string". Useful for anyone using java / regex.
The whole-input rule of matches means a pattern that appears in the middle of a string is rejected. Switching to find, or padding the pattern with leading and trailing wildcards, turns it into a containment check.
Good to see "matches requires the whole string" written down. Handy reference for anyone working with java / regex.
Appreciate the write-up on "matches requires the whole string". Useful for anyone using java / regex.
Specific and to the point: "matches requires the whole string". Worth a look if you touch java / regex.
"matches requires the whole string" is a useful thing to know about with java / regex. Thanks for sharing it.