Reputation: 45
I want a regular expression to check that:
A password contains exactly one uppercase character, includes at least one special character (#, @, -
) and password length must be at least 8 symbols!
(?=.[A-Z]{1}?)(?=.*[#@-])[A-Za-z#@-]{8,}
How can I write it for a password contains exactly one uppercase character?
Upvotes: 2
Views: 1514
Reputation: 4302
Try this:
^(?=(?=.*[@#-]{1,})[\S]{8,})([a-z@#-]*[@#-]*[A-Z][a-z@#-]*[@#-]*)$
https://regex101.com/r/v8Erhu/1
Upvotes: 0
Reputation: 135792
Try this one:
^(?=[^A-Z]*[A-Z][^A-Z]*$)(?=.*[#@-])[A-Za-z#@-]{8,}$
Demo: https://regex101.com/r/Fr6znT/1
Breakdown:
(?=[^A-Z]*[A-Z][^A-Z]*$)
at the position it is means: "The whole expression should be 'any number of non uppercase chars', followed by 'one uppercase char', followed by 'any number of non uppercase chars'".Upvotes: 1