简体   繁体   中英

Regex 2 Numbers OR Special Characters

So i'm trying to create a Regex which does the following:

Min 12 Characters, Requires Uppercase, Requires Lowercase, Requires 2 Numeric values OR 2 Special Characters.

At the moment i have the following:

~^(?=\P{Ll}*\p{Ll})(?=\P{Lu}*\p{Lu})(?=.*[!@#$%^&*()]|\D*\d).{12,}~u

Which does 1 numeric OR 1 special character, not 2. I've tried adding {2} to the OR condition, however, this requires a combination of two which is incorrect.

Any help would be appreciated.

You should replace (?=.*[!@#$%^&*()]|\\D*\\d) lookahead with (?:(?=(?:[^!@#$%^&*()]*[!@#$%^&*()]){2})|(?=(?:\\D*\\d){2})) . The regex will look like

'~^(?=\P{Ll}*\p{Ll})(?=\P{Lu}*\p{Lu})(?:(?=(?:[^!@#$%^&*()]*[!@#$%^&*()]){2})|(?=(?:\D*\d){2})).{12,}$~u'

See the regex demo .

The lookahead matches a location that is immediately followed with

  • (?:[^!@#$%^&*()]*[!@#$%^&*()]){2} - two repetitions of any 0+ chars other than !@#$%^&*() chars followed with a char from the !@#$%^&*() list
  • | - or
  • (?=(?:\\D*\\d){2} - two repetitions of any 0+ non-digit chars followed with a digit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM