Delfin
Delfin

Reputation: 607

Restrict some charcters in special characters

I have tried regular expression that considers only statements that have zero or more occurances of "%" and "&" and returns false if "@" or "$" is present. :

^((%&)*(?!@).)*$

What I need is a regular expression that validates only those strings that must have 2 or more special characters from the set (%&) and return false if any other special character is present.

Upvotes: 2

Views: 65

Answers (1)

kfairns
kfairns

Reputation: 3057

(?=(?:.*[%&]){2,})(?!.*[@$])^.*$

Positive lookahead to make sure % or & occur at least 2 times

Negative lookahead to make sure @ and $ don't occur at all

Edit

Now makes sure to match entire string (^, $)

Check it out here

Upvotes: 1

Related Questions