Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex: Accept digits and atleast 1 star

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading