Reputation: 1617
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 advance.
Upvotes: 6
Views: 235
Reputation: 1617
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.
Upvotes: 3
Reputation: 78423
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]
Upvotes: 2
Reputation:
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.
Upvotes: 0
Reputation: 21172
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.
Upvotes: 0