Girish Chaudhari
Girish Chaudhari

Reputation: 1617

What is the regular expression for this "password"?

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

Answers (4)

Girish Chaudhari
Girish Chaudhari

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

Denis de Bernardy
Denis de Bernardy

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

user166390
user166390

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

Zachary Scott
Zachary Scott

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

Related Questions