What would be the correct regex, to satisfy the following password criteria:
Must include at least 1 lower-case letter.
Must include at least 1 upper-case letter.
Must include at least 1 number.
Must include at least 1 special character (only the following special characters are allowed: !#%).
Must NOT include any other characters then A-Za-z0-9!#% (must not include ; for example).
Must be from 8 to 32 characters long.
This is what i tried, but it doesn't work:
^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[\!\#\#\$\%\&\/\(\)\=\?\*\-\+\-\_\.\:\;\,\]\[\{\}\^]).{8,32}
But it should be:
^(?=.*?[a-z])(?=.*?[A-Z])(?=.*?[0-9])(?=.*?[\!\#\#\$\%\&\/\(\)\=\?\*\-\+\-\_\.\:\;\,\]\[\{\}\^])[A-Za-z0-9!#%]{8,32}
But Unihedron's solution is better anyways, just wanted to mention this for the users which will read this question in the future. :)
Unihedron's solution (can also be found in his answer below, i copied it for myself, just in case he changes (updates it to an better version) it in his answer):
^(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z])(?=\D*\d)(?=.*?[!#%])[A-Za-z0-9!#%]{8,32}$
I ended up with the following regex:
^(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z])(?=\D*\d)(?=.*?[\!\#\#\$\%\&\/\(\)\=\?\*\-\+\-\_\.\:\;\,\]\[\{\}\^])[A-Za-z0-9\!\#\#\$\%\&\/\(\)\=\?\*\-\+\-\_\.\:\;\,\]\[\{\}\^]{8,60}$
Thanks again Unihedron and skamazin. Appreciated!
Use this regex:
/^(?=[^a-z]*[a-z])(?=[^A-Z]*[A-Z])(?=\D*\d)(?=[^!#%]*[!#%])[A-Za-z0-9!#%]{8,32}$/
Here is a regex demo!
Read more:
Regex for existence of some words whose order doesn't matter
Test your possible passwords on this and see if they give you the proper result
The regex I used is:
^(?=.*[a-z])(?=.*[A-Z])(?=.*?[0-9])(?=.*?[!#%])[A-Za-z0-9!#%]{8,32}$
Related
I need to create a regular expression to check if a password has at least 1 uppercase letter, at least 2 numbers, and ends with a $ (dollar sign).
I've been trying to figure it out, but I can only get as far as checking if there's at least 1 uppercase and one number, rather than two.
These should be valid:
4hg5Fjkjk$
fh##Y5fFF5$
hgH5Hu6$
These should not be valid:
45tyghisu$ (No capital)
5THygfhy$ (Only one number)
Gh%hF45$h (No dollar sign at the end)
Here's what I have so far (checks for at least one number, one capital and dollar sign at the end)
/(?=.*[A-Z])(?=.*\d).*\$/
Any help would be greatly appreciated!
ps. I've looked on SO, and can't find anything relating to more than one required character.
In your pattern you have to repeat asserting a digit twice instead of one time using for example (?=(?:[^\d\r\n]*\d){2}) using contrast.
If you don't want to allow spaces in the password, you could use \S+ to match 1+ times a non whitespace char.
You could use:
^(?=[^A-Z\r\n]*[A-Z])(?=(?:[^\d\r\n]*\d){2})\S+\$$
Regex demo
According to the given answer by the OP, the number of characters should be 9-15:
^(?=[^A-Z\r\n]*[A-Z])(?=(?:[^\d\r\n]*\d){2})\S{9,15}\$$
Regex demo
This RegEx is simple and makes no other assumptions about what characters may be in a password other than what was specified by the OP.
^(?=.*?[A-Z])(?=.*?\d.*?\d).*\$$
See Demo (Click ON "RUN TESTS")
thanks for all the answers. Looked through them, and I figured out a pretty simple way to do it using the regular expression below. I edited it to allow for setting a length on the password, just change the 9 and 15 to your desired lengths.
/^(?=.*[A-Z])(?=.*\d.*\d)[^\s]{9,15}\$$/
I need to check if a XML document contains some software build numbers
normally they're like ######, where:
First two characters are numbers
Third character is a letter in uppercase
last three characters are numbers
Example: 10B329 or 11A465.
But there could be some exceptions, like 8L1 or 11B465a. (if there's another character after the sixth, it's always a letter in lowercase).
I think they're always with a minimum length of 3 characters and a maximum length of 7 characters.
So what could be the best pattern to match? I tried this but it doesn't work since it takes also words...
Dim BuildPattern As String = "<key>[0-9A-Z]*</key>"
Try this Regex: \d{2}\w\d{3}
You can see a live demo here.
You can add the <key></key> tags too: <key>(\d{2}\w\d{3})<\/key>. This way, your match will be in group 1 of the match. Changed demo.
Note that you should rather use XML parser for this as it's safer and more accurate than working with regex on XML files.
EDIT: Can't help you with the non-standard length though, my knowledge of regexes is still too low. Perhaps you really should try XML parser instead?
Working on password-field.
The regex expression for a field container letters (lowercase, uppercase), digits and some special characters will look like this:
^([a-z,A-Z,0-9,#,$,%,&,_,]{8,20})*$
Tell me please, how this should be modified if I want every pass phrase to have at least one lowercase, one uppercase and one digit?
For example, for 3-characters long pass it is:
'aB3' - pass
'ab3' - fail
You need to use lookaheads and also you need to remove all the commas present inside the character class.
^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?\d)[a-zA-Z0-9#$%&_]{8,20}$
Note that the password must be atleast 8 and atmost 20 chars long.
DEMO
THIS ANSWER HAS BEEN ARCHIVED BECAUSE IT SUCKS.
Use the + token, which tells the engine to attempt to find one or more of the preceding token. e.g:
^(?=[a-z]+)(?=[A-Z])+(?=[0-9])+(?=[#$%&_\,\.]*)$
Then, use a length check elsewhere in the code to verify length.
So I wanted to limit a textbox which contains an apartment number which is optional.
Here is the regex in question:
([0-9]{1,4}[A-Z]?)|([A-Z])|(^$)
Simple enough eh?
I'm using these tools to test my regex:
Regex Analyzer
Regex Validator
Here are the expected results:
Valid
"1234A"
"Z"
"(Empty string)"
Invalid
"A1234"
"fhfdsahds527523832dvhsfdg"
Obviously if I'm here, the invalid ones are accepted by the regex. The goal of this regex is accept either 1 to 4 numbers with an optional letter, or a single letter or an empty string.
I just can't seem to figure out what's not working, I mean it is a simple enough regex we have here. I'm probably missing something as I'm not very good with regexes, but this syntax seems ok to my eyes. Hopefully someone here can point to my error.
Thanks for all help, it is greatly appreciated.
You need to use the ^ and $ anchors for your first two options as well. Also you can include the second option into the first one (which immediately matches the third variant as well):
^[0-9]{0,4}[A-Z]?$
Without the anchors your regular expression matches because it will just pick a single letter from anywhere within your string.
Depending on the language, you can also use a negative look ahead.
^[0-9]{0,4}[A-Za-z](?!.*[0-9])
Breakdown:
^[0-9]{0,4} = This look for any number 0 through 4 times at the beginning of the string
[A-Za-z] = This look for any characters (Both cases)
(?!.*[0-9]) = This will only allow the letters if there are no numbers anywhere after the letter.
I haven't quite figured out how to validate against a null character, but that might be easier done using tools from whatever language you are using. Something along this logic:
if String Doesn't equal $null Then check the Rexex
Something along those lines, just adjusted for however you would do it in your language.
I used RegEx Skinner to validate the answers.
Edit: Fixed error from comments
I am using this regex to validate my password.
My password -
should be alphanumeric ONLY,
contains at least 8 characters,
at least 2 numbers
and at least 2 alphabet.
My regex is
^.*(?=.{8,})(?=.*\d*\d)(?=.*[a-zA-Z]*[a-zA-Z])(?!.*\W).*$
but unfortunately it still matches if I try to put special characters at the beginning.
For example #password12, !password12.
Because your pattern begins and ends with .*, it will match anything at the beginning or end of the string, including special characters.
You shouldn't be solving this problem with a single regular expression, it makes the code hard to read and hard to modify. Write one function for each rule using whatever makes sense for that rule, then your validation script becomes crystal clear:
if is_alpha_only(password) &&
len(password) > = 8 &&
has_2_or_more_numbers(password) &&
has_2_or_more_alpha(password) ...
Seriously, what's the point of cramming all of that into a single regular expression?
And why disallow special characters? There's simply no reason for that.
You can use the following regex in case insensitive mode:
^(?=[a-z]*[0-9][a-z]*[0-9])^(?=[0-9]*[a-z][0-9]*[a-z])[a-z0-9]{8,}$
See it
I had a similar situation in which the client needed 4 alpha, 1 number, and between 8 and 20 characters. I've adapted my solution to your problem:
^(?=(?:[a-zA-Z0-9]*[a-zA-Z]){2})(?=(?:[a-zA-Z0-9]*\d){2})[a-zA-Z0-9]{8,}$
I understand the other answers dissuading you from this route, but sometimes the client wants what the client wants, regardless of your arguments to the contrary.