LightningWar
LightningWar

Reputation: 975

Regex to permit only certain characters

I have a working regex that matches at least 1 uppercase letter, 1 lowercase letter and 1 digit:

"(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*"

I'm trying to modify to only permit an underscore, dollar sign or pound sign, _ $ £ I'm not sure how to approach this. This is not correct:

"(?=.*\d)(?=.*[a-z_$£])(?=.*[A-Z]).*"

For example:

'Pa$$w0rd' - true
'orange1_' - false
'Apple22_' - true
'Banana100_!' - false

What is wrong with the example?

Upvotes: 1

Views: 159

Answers (1)

tobias_k
tobias_k

Reputation: 82899

Your regex already permits all those characters. If I understand your question correctly, you want to permit only those special characters (besides letters and digits). In this case, you should change the .* at the end of your regex to [\d\w$£]* (_ is included in \w):

^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[\d\w$£]*$

Online demo: https://regex101.com/r/6ZpaD2/2

Upvotes: 1

Related Questions