Mahmoud
Mahmoud

Reputation: 217

Regex to check for at least 8 digits and exactly 2 characters

I'd like to make an regular expression that check in a string that match at least 8 digits and exactly 2 characters in uppercase. the letters and decimals can be anywhere.

Thanks

Upvotes: 1

Views: 1217

Answers (1)

JvdV
JvdV

Reputation: 76000

Would this work:

^(?=[A-Z0-9]{10,})\d*[A-Z]\d*[A-Z]\d*$
  • ^ - Match start of string
  • (?=[A-Z0-9]{10,}) - A positive LookAhead to match at least 10 chars from [A-Z0-9] only
  • \d*[A-Z]\d*[A-Z]\d* - Two chars in the range [A-Z] surrounded by zero or more digits
  • $ - Match end of string

Alternatively, create another capture group:

^(?=[A-Z0-9]{10,})(\d*[A-Z]){2}\d*$

Now you got a little bit more flexibility to tell the pattern you need an x-amount of upper case letters > {x}

Upvotes: 5

Related Questions