Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

regex - Need regular expression for checking at least 3 uppercase, 3 lowercase, 3 digits and 3 special character

Need regular expression for checking at least 3 uppercase, 3 lowercase, 3 digits and 3 special character any where in string.

I have tried /^(?=.*[^A-Za-z0-9]{3,})(?=.*[A-Z]{3,})(?=.*d{3,})(?=.*[0-9]{3,}).+/ but this checking contiguous string like :: abcABC123(*) but did not check like: 1a(2b)AB*3cC

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You were close: you need to bracket the .* with the character classes in your look aheads:

^(?=(.*[^A-Za-z0-9]){3})(?=(.*[A-Z]){3})(?=(.*d){3}).+

The reason this works is that the character types may not be adjacent, eg 3 digits might be a1b2c3, hence the .* to allow other intervening character types.

Note that you don't need the open-ended quantifiers. eg (.*d){3} is sufficient to assert that there are at least 3 digits - ie not ...{3,}


And a final note: those leading/trailing slashes have nothing whatsoever to do with regular expressions - they are an application language artefact. It makes questions and answers clearer and more useful to more people if they are omitted.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...