开发者

What is the regular expression for this "password"?

开发者 https://www.devze.com 2023-03-06 18:22 出处:网络
I want regular expression that contains at least one alphanumeric character, at least one non alphanumeric character (special character) and at least one digit.

I want regular expression that contains at least one alphanumeric character, at least one non alphanumeric character (special character) and at least one digit.

Thanks in advanc开发者_开发技巧e.


Correct answer of my question :

regular Expression :

^((?=.[\d])(?=.[a-z])(?=.[A-Z])|(?=.[a-z])(?=.[A-Z])(?=.[^\w\d\s])|(?=.[\d])(?=.[A-Z])(?=.[^\w\d\s])|(?=.[\d])(?=.[a-z])(?=.[^\w\d\s])).{8,30}$

Thanks.


So, in other words, at least one alpha, one digit, and one non-alphanum...

You need two lookaheads:

(?=.*[a-zA-Z])(?=.*[0-9]).*[^a-zA-Z0-9]

Since this is marked as homework, I suggest you actually understand it, too:

http://www.regular-expressions.info/lookaround.html


In case you don't want spaces either:

(?=.*[a-zA-Z])(?=.*[0-9]).*(?=\S)[^a-zA-Z0-9]


It would be easy to pick out any one of them, but to have all three of them in any order would be something. You could try a single character of one of the three then use look aheads for the remaining, but you would still need to check behind that character too.


Since this is homework (or perhaps not?), here is a hint for a solution that doesn't use look-aheads or other nifty tricks.

In how many (and it what) permutations can the characters occur? How can this be used with the alternation (|) operator?


I would use multiple tests and not a single regex :-)

Happy coding.

0

精彩评论

暂无评论...
验证码 换一张
取 消