2
votes

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?

2
Before doing this, you should read security.stackexchange.com/questions/32222/… and consider whether it actually makes sense.Ilmari Karonen

2 Answers

1
votes

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'".
1
votes

Try this:

^(?=(?=.*[@#-]{1,})[\S]{8,})([a-z@#-]*[@#-]*[A-Z][a-z@#-]*[@#-]*)$

https://regex101.com/r/v8Erhu/1