-4

I am using p{Digit} to validade a String. However when I use "101ᶁ1" the result is true. This is happening with some symbols: ᶁ,ﻹ

Pattern p = Pattern.compile("[\\p{Digit}]");
boolean result = p.matcher(value).find();

I didn't find the characters that are validated in the documentation.

Sergei Sirik
  • 1,249
  • 1
  • 13
  • 28

1 Answers1

1

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}+");
user85421
  • 28,957
  • 10
  • 64
  • 87
  • Indeed, I need to check if the character isn't a digit and isn't a punctuation and isn't a latin character. In another words, I need that return false when the string contains ᶁ, for example. I'm trying this pattern: Pattern p = Pattern.compile("^\\p{IsLatin}\\p{Digit}\\p{Punct}+$");. But, don't work. – Rosicleia Frasson Aug 05 '19 at 16:22