I am trying to use this regex
.*[!##$%^&*()].*[0-9]|[0-9].*[!##$%^&*()].*
to make my passwords have at least 1 special character and 1 number (which it does). But I am trying to also have it take passwords like mike1# .
How can I modify it so it takes letters like that as well?
Its better to do this using lookaheads.
^(?=.*[!##$%^&*()])(?=.*[0-9])(?=.*[a-zA-Z]).*$
The problem with your approach is you will have to cover all possible combinations where in a letter comes first,or a number comes first.Using lookahead which are 0 width assertions are just a type of check,we can fail the regex if it does not have even a single special character or number or letter.
(?=.*[a-zA-Z])
^^ ^^^^^^^
The lookahead simply states that for this regex to pass there should be at least one letter somewhere.
EDIT:
^(?=.*[!##$%^&*()])(?=.*[0-9]).*$
Use this if letter constraint is not present.
Related
I'm having some trouble creating a regex for a password. It has the following requirements :
at least 10 characters
at least 1 uppercase letter
at least 1 lowercase letter
at least 1 special character
I currently created this line :
`
"^(?=(.*[A-Z])+)(?=(.*[a-z])+)(?=(.*[0-9])+)(?=(.*[!##$%^&*()_+=.])+){10,}$"
`
For a password like : Lollypop56#
it still gives me false.
You forgot a full point after the lookahead of special characters. So it would be:
"^(?=(.*[A-Z])+)(?=(.*[a-z])+)(?=(.*[0-9])+)(?=(.*[!##$%^&*()_+=.])+).{10,}$"
I recommend to you that use https://www.passay.org/. This dependency able you to validate wide range of password policy.
I'd remove the + in the groups inside the lookaheads since those aren't needed and also make those groups non-capturing. I'd also not explicitly specify the characters in the "special" group. Just make it match any of the characters not in the first three groups, [^A-Za-z0-9]. That'll allow ~ as a special character too etc.
Also, the actual matching should be .{10,}, not {10,}.
"^(?=(?:.*[A-Z]))(?=(?:.*[a-z]))(?=(?:.*[0-9]))(?=(?:.*[^A-Za-z0-9])).{10,}$"
Can anyone tell how I can write regex for a string that take one or more alphanumeric character followed by an even number of digits?
Valid:
a11a1121
bbbb11a1121
Invalid:
a11a1
I have tried ^[a-zA-Z*20-9]*$ but it is always giving true.
Can you please help in this regard?
The regex that you have mentioned will search for any number of [either a-z, or A-Z or 2 or 0-9]
You can break down your requirement to groups and then handle it accordingly.
Like you require at least one character. so you start with ^([a-zA-Z]+)$
Then you need numbers in the multiple of 2. so you add ^([a-zA-Z]+(\d\d)+)$
Now you need any number of combination of these. So the exp becomes: ^([a-zA-Z]+(\d\d)+)*$
You can use online tools like regex101 for these purpose. The provided regex in action here
You can achieve it with this regexp: ^[a-z0-9]*[a-z]+([0-9]{2})*$
Explanation :
[a-z0-9]*[a-z]+: a string of at least one character terminated by a non digit one
([0-9]{2})*: an odd sequence of digits (0 or 2*n digits). If the even sequence cannot be null, use ([0-9]{2})+ instead.
I am trying to figure out a regex to match a password that contains
one upper case letter.
one number
one special character.
and at least 4 characters of length
the regex that I wrote is
^((?=.*[0-9])(?=.*[A-Z])(?=.*[^A-Za-z0-9])){4,}
however it is not working, and I couldn't figure out why.
So please can someone tell me why this code is not working, where did I mess up, and how to correct this code.
Your regex can be rewritten as
^(
(?=.*[0-9])
(?=.*[A-Z])
(?=.*[^A-Za-z0-9])
){4,}
As you see {4,} applies to group which doesn't let you match any character since look-around is zero-width, which effectively means "4 or more of nothing".
You need to add . before {4,} to let your regex handle "and at least 4 characters of length" point (rest is handled by look-around).
You can remove that capturing group since you don't really need it.
So try with something like
^(?=.*[0-9])(?=.*[A-Z])(?=.*[^A-Za-z0-9]).{4,}
You could come up with sth. like:
^(?=.*[A-Z])(?=.*\d)(?=.*[!"ยง$%&/()=?`]).{4,}$
In multiline mode, see a demo on regex101.com.
This approach specifies the special characters directly (which could be extended, obviously).
From the following list only the bold ones would satisfy these criteria:
test
Test123!
StrongPassword34?
weakone
Tabaluga"12???
You can still enhance this expression by being more specific and requiring contrary pairs. Just to remind you, the dot-star (.*) brings you down the line and then backtracks eventually. This will almost always require more steps than to directly look for contrary pairs.
Consider the following expression:
^ # bind the expression to the beginning of the string
(?=[^A-Z\n\r]*[A-Z]) # look ahead for sth. that is not A-Z, or newline and require one of A-Z
(?=[^\d\n\r]*\d) # same construct for digits
(?=\w*[^\w\n\r]) # same construct for special chars (\w = _A-Za-z0-9)
.{4,}
$
You'll see a significant reduction in steps as the regex engine does not have to backtrack everytime.
i need to validate a reference code that is in the form of 2 letters, 3 numbers and one letter. I've tried using for functions, and have also tried going through each letter individually but its either too messy or it's not giving me the result i want. I've heard you can use regex but i'm not sure how to apply it. Any help would be great. Thanks
I was told that this works:
reference.matches("[A-z]{2}+[0-9]{3}+[A-z]")
but when i run it, no matter what i put into the console that is is incorrect.
that is in the form of 2 letters, 3 numbers and one letter
You can use this regex:
reference.matches("(?i)[A-Z]{2}[0-9]{3}[A-Z]");
Explanation:
(?i) - for ignore case matching
[A-Z]{2} - for matching 2 letters
[0-9]{3} - for matching 3 digits
[A-Z] for matching single letter
If I have a set of characters like "abcdefghij" and using this characters, I generate random a password using this characters. A generated password can have, for example, 6 characters. How to validate a password using regex so that tho neighbor characters are not identical and a character does not repeat more that twice?
You could use something like:
/^
(?:(.)
(?!\1) # neighbor characters are not identical
(?!(?>.*?\1){2}) # character does not occur more than twice
)*
\z/x
Perl quoting, the atomic group can be removed if not supported.
In Java regex it could be written like:
^(?:(.)(?!\1|(?:.*?\1){2}))*\z
AFAIK, this cannot be done with a simple regexp (particularly, ensuring that a letter only appears twice at max. You could do a bunch of expressions like
[^a]*(a[^a]*(a[^a]*))
[^b]*(b[^b]*(b[^b]*))
....
and also (matching means the validation failed):
[^a]*aa[^a]*
[^b]*bb[^b]*
but obviously this is not good idea.
The condition that the characters do not repeat together maybe can treated with capturing groups, but I am almost sure the other one cannot be checked with regex.
BTW... why the obsession with regex? Programming these checks are trivial, regex is useful in a set of cases but not every check can be done with regex.