Why doesn’t this regex work as expected in Java?

Matcher.matches() checks to see if the entire input string is matched by the regex.

Since your regex only matches the very first character, it returns false.

You’ll want to use Matcher.find() instead.

Granted, it can be a bit tricky to find the concrete specification, but it’s there:

Leave a Comment