Testing regex with Java
The string hhh
contains two h
s, therefore the regex matches since the find()
method allows matching of substrings.
If you anchor the regex to force it to match the entire string, the regex will fail:
^h{2}$
Another possibility would be to use the matches()
method:
"hhh".matches("h{2}")
will fail.