String should contain digits and atleast one asterisk. Please help me with the regex.
System.out.println(Pattern.matches("\\d", "12345678*")); //true
System.out.println(Pattern.matches("\\d", "1234****")); //true
System.out.println(Pattern.matches("\\d", "123456789")); //false
System.out.println(Pattern.matches("\\d", "abc45678*")); //false
what should be the correct regex pattern ?
I tried different patterns like [0-9](?=.*_)
. but no luck.
Thank you!
CodePudding user response:
You can use
.matches("\\d \\* ", "123456789")
Here, the regex matches the whole string that starts with one or more digits and one or more asterisks after these digits.
See the regex demo.