Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex to allow atleast one special character, one uppercase, one lowercase(in any order)

Tags:

regex

Can anyone help me with a regex to allow atleast one special character, one uppercase, one lowercase.

This is what I have so far:

 ^.*(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$

but it seems to match the characters only in the order "special character", "uppercase", "lowercase".

Any help is greatly appreciated

like image 743
Sweta Avatar asked May 11 '12 19:05

Sweta


People also ask

What is the regex for special characters?

Special Regex Characters: These characters have special meaning in regex (to be discussed below): . , + , * , ? , ^ , $ , ( , ) , [ , ] , { , } , | , \ . Escape Sequences (\char): To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ).

Which one is the correct regular expression to check if a string contains at least one uppercase English alphabet?

*[A-Z]) represents at least one uppercase character.

How do you match upper and lower cases in regex?

Using character sets For example, the regular expression "[ A-Za-z] " specifies to match any single uppercase or lowercase letter. In the character set, a hyphen indicates a range of characters, for example [A-Z] will match any one capital letter.


1 Answers

Your regex

^.*(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$

should actually work just fine, but you can make it a lot better by removing the first .*:

^(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$

will match any string of at least 8 characters that contains at least one lowercase and one uppercase ASCII character and also at least one character from the set @#$%^&+= (in any order).

like image 103
Tim Pietzcker Avatar answered Oct 03 '22 07:10

Tim Pietzcker