I believe you misunderstood the usage of find()
. It searches for any the first occurrence of the regular expression in the searched text. (Pattern.start()
returns the position where the expression was found)
The expression "[\\p{Digit}]"
- the []
do nothing here - is just matching ONE digit. Since the searched text has a digit, the result of find()
is true
.
To match the whole text, the expression must start with ^
to match the beginning of the text and end with $
corresponding to the end of the text. And it must allow more than one digit, so it needs an +
(one or more) resulting in
Pattern p = Pattern.compile("^\\p{Digit}+$");
boolean result = p.matcher(value).find();
matches()
can be used to test against the whole text, so ^
and $
are not needed - still needs a +
to allow more than one digit:
Pattern p = Pattern.compile("\\p{Digit}+");
boolean result = p.matcher(value).matches();
Note: this can written as:
boolean result = value.matches("\\p{Digit}+");