When using matches()
, you are asking if the string you provided matches your regex as a whole. It is as if you added ^
at the beginning of your regex and $
at the end.
Your regex is otherwise fine, and returns what you expect. I recommend testing it regexplanet.com, Java mode. You will see when matches()
is true, when it false
, and what each find()
will return.
To solve your problem, I think you only need to remove the if (matcher.matches())
condition.