I wanna test my own regex in order to understand how can i use it and create my custom regex.
I tried this one :
^(?=.*\d)(?=.*[a-zA-Z])(?=.*[\W_]){3}$
(?=.*\\d)
=> at least 1 number. (?=.*[a-zA-Z])
=> at least 1 letter. (?=.*[\\W_])
=> at least 1 special character. {3}
=> at least 3 characters.
Unfortunately, it doesn't work, but i want at least 1 number, 1 letter and 1 special character, and at least 3 characters in my input. When the user types those 3 types of characters, my message disappears because the regex is correct ^^
Sorry for my bad english, I can give you more details if you want and thank you for the help :)
Have a nice day :)
The problem is that your {3}
quantifier applies to your last look-ahead, which is nonsensical : it means that at the start of the text, you must match 3 times the look-ahead, which is a given if it matches once since lookaround are 0-width matches.
You could use the following :
^(?=.*\d)(?=.*[a-zA-Z])(?=.*[\W_]).{3,}$
which specifies, aside from your existing look-aheads, that at least 3 characters must be matched.
If you just test the string, it would also be enough to match
^(?=.*\d)(?=.*[a-zA-Z])(?=.*[\W_]).{3}
without an end anchor : you match 3 characters, and stop matching what follows since your requisites are met.
If you want 1 digit, 1 alpha, 1 special character, those are already at least 3 characters .
^(?=\D*\d)(?=.*?[a-zA-Z]).*[\W_].*$
Here's a demo at regex101 . Or for only matching:
^(?=\D*\d)(?=.*?[a-zA-Z]).*[\W_]
(?=\\D*\\d)
The first lookahead requires one digit (preceded by any \\D
non-digits ). (?=.*?[a-zA-Z])
second lookahead requires one alpha (preceded by any characters). .*[\\W_]
matching until one special character. All together requires at least 3 different characters: 1 digit, 1 alpha, 1 special.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.