Home > Software engineering >  Regex for string to contain at least one letter and number
Regex for string to contain at least one letter and number

Time:12-03

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 enter image description here

CodePudding user response:

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

  • Related