In a Java application I need to check if a given string
- contains only digits 0-9 and
- is exactly five digits long
My first attempt is this:
public static void main(String[] args) {
String testString = "000000";
String myPattern = "\\d{5}";
Pattern validCharacterPattern = Pattern.compile(myPattern);
Matcher matcher = validCharacterPattern.matcher(testString);
boolean b = matcher.find();
if (b) System.out.println("Valid");
else System.out.println("Invalid");
}
However the above expression is also true for e.g. 123456
. What do I need to change?