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 for string to contain at least one letter and number

This regex expression will match the specified number of word characters, with a space in either side:

(?<=\s)(?:\w){12}(?=\s)

How can I modify this expression so that it returns only the string containing the mixed-alphanumeric result containing at least one letter and at least one number? Here is the current Regex Demo.

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

enter image description here

>Solution :

Try:

\b(?=[^\s]*\d)(?=[^\s]*[a-zA-Z])\w{12}\b

Regex demo.


\b – word boundary

(?=[^\s]*\d) – continue matching if ahead is a number preceded with any amount of non-space characters.

(?=[^\s]*[a-zA-Z]) – the same with letters

\w{12} – match 12 word characters

\b – word boundary

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