I want to check if string doesn’t have more than 5 numbers. I can do it this way:
Matcher matcher = Pattern.compile("\\d").matcher(val);
i = 0;
while (matcher.find()) {
i++;
}
However I would like to do it without while (because we are using regex validation framework).
I want to be able to match strings like
A2sad..3f,3,sdasad2..2
This regex matches strings containing a max of 5 digits:
And if you want to match strings consisting of exactly 5 digits, do:
Note that inside a java.lang.String literal, you’ll need to escape the backslashes:
or:
and you don’t need to add the “anchors”
^and$since Java’smatches(...)already does a full match of the string.