S_Nissan
S_Nissan

Reputation: 79

Regex for 1 uppercase and 2 lowercase letters, 2 digits, and 2 non-alphanumeric characters

I'm trying to create a Regex for a string that contains 1 uppercase and 2 lowercase letters, 2 digits, and 2 non-alphanumeric characters, for now I have:

(?=.*\d{2})(?=.*[a-z]{2})(?=.*[A-Z])(?=.*\W{2})

The problem is that the chars must be tougher in this regex and that's not the case I need.

I need solution that accept:

Taa12@!
T1a1b@!
a1!b@2A

Upvotes: 1

Views: 1248

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626728

You can use

^(?=(?:\D*\d){2})(?=(?:[^a-z]*[a-z]){2})(?=[^A-Z]*[A-Z])(?=(?:\w*\W){2})

See the regex demo.

Details:

  • ^ - start of string
  • (?=(?:\D*\d){2}) - at least two digit chars (not necessarily consecutive)
  • (?=(?:[^a-z]*[a-z]){2}) - at least two lowercase ASCII letters (not necessarily consecutive)
  • (?=[^A-Z]*[A-Z]) - at least one ASCII letter
  • (?=(?:\w*\W){2}) - at least two non-word chars (not necessarily consecutive)

Upvotes: 3

Related Questions