user3675875
user3675875

Reputation: 1

Regex matching unwanted special Chars

Could someone explain to me why the following regex

^(?=.*\w)(?=.*[!@#$*_])(?!.*\s).{8,20}$

match : test*~|-*test

I'm trying to validate the following rules:

Upvotes: 0

Views: 54

Answers (1)

Gregory Adam
Gregory Adam

Reputation: 117

Your pattern requires

(1) (?=.*\w) at least one word char

(2) (?=.[!@#$_]) at least one of those special chars

(3) (?!.*\s) no space char

(4) .{8,20} any char, from 8 to 20 long

Your input test*~|-*test matches the pattern

For what you're after I'd use

^(?=.*[!@#$*_])(?=.*\w)[\w!@#$*_]{8,20}$

(1) (?=.[!@#$_]) at least one special char

(2) (?=.*\w) at least one word char

(3) [\w!@#$*_]{8,20} word char or special char, 8 to 20 times

Upvotes: 1

Related Questions