Changing existing regex so at least 1 numerical value is required

Viewed 18

Said regex: \b(?=(?:[a-z\d]*[A-Z]){3})(?=.*\d)(?=(?:[A-Z\d]*[a-z]){2})[a-zA-Z\d]{5,30}\b

I am trying to add just 1 condition to this regex, that being that 1 number is required in order to match, I have tried inserting the lookahead (?=.*\d) but it did not work, as it matches the "HeLLoWoRlD" portion of <HeLLoWoRlD"123

1 Answers

You may add the (?=[A-Za-z]*\d) lookahead check:

\b(?=[A-Za-z]*\d)(?=(?:[a-z\d]*[A-Z]){3})(?=(?:[A-Z\d]*[a-z]){2})[a-zA-Z\d]{5,30}\b
  ^^^^^^^^^^^^^^^

See the regex demo.

The (?=[A-Za-z]*\d) lookahead matches a location that is immediately followed with 0 or more ASCII letters and then one digit.

Related