I'm trying to create a regex for a password input, where the user must enter at least one digit, one uppercase letter, one lowercase letter, and any one symbol except the asterisk and percentage sign, and must be at least fifteen characters long. Thus far, I have come up with this:
(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^A-Za-z\d*%]).{15,}
But when I test this on RegexR, I try inputting the following string, ILovePizza1234!!!* and it passes. What is wrong with the expression? Please help, and thanks for any tips in advance
Your lookahead assertion (?=.*[^A-Za-z\d*%]) checks if there is at least one character except alphanumericals, percent signs or asterisks. It does not prohibit the presence of any asterisk/percent sign. So as long as there is at least one character that matches [^A-Za-z\d*%] (a condition fulfilled by ! in your test string), that assertion succeeds.
You need an additional negative lookahead assertion:
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^A-Za-z\d])(?!.*[*%]).{15,}
(?!.*[*%]) will cause the regex match to fail if a * or % is present anywhere in the string.
Related
I have a string that the following structure:
ABCD123456EFGHIJ78 but sometimes it's missing a number or a character like:
ABC123456EFGHIJ78 or
ABCD123456E or
ABCD12345EFGHIJ78
etc.
That's why I need regular expressions.
What I want to extract is the first letter of the third group, in this case 'E'.
I have the following regex:
(\D+)+(\d+)+(\D{1})\3
but I don't get the letter E.
This seems to work for the example cases you provided.
^(?:[A-Za-z]+)(?:\d+)(.)
It assumes that the first group is only letters and that the second group is only digits.
There's already a nice answer.
But for the records, your initial proposal was very close to work. You just needed to say that the character matching the 3rd group can repeat several times by adding a star:
^(\D+)(\d+)(\D{1})\3*
The main weakness is that \D matches any char except digits, so also spaces. Making it more robust leads us to explicit the range of chars accepted:
^([A-Za-z]+)(\d+)([A-Za-z]{1})\3*
It's much better, but my favourite uses \w to match at the end of the pattern any non white character:
([A-Za-z]+)(\d+)([A-Za-z]{1})\w*
I am new to regex and trying to find a regex, which will allow Alphanumeric or text but not complete numeric with special characters like "," , "'","()" ,"/" ,".".
ex: aa12,--allowed
aa,-allowed
123-Not allowed
123,--not allowed
123,a--allowed.
Alpha Numeric-
\d*[a-zA-Z][a-zA-Z\d\s]*$
is working but stuck with including special characters. Any help?
If you simply want to make sure the string contains a character, just match with
.*[a-zA-Z].*
if you need to match the full line. Or even simpler
[a-zA-Z] or [a-z]
The last if you can set the case insensitive flag (you haven't stated regex flavor). These last two will simply match if there's a letter in the text. And I can't see that what you're looking for is anything but that.
See it here at regex101.
This works for me in all scenarios..
/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[_#.#&+-])[A-Za-z0-9_#.#&+-]{8,}$/
Here is how it works,
^(?=.*\d) - checks for at-least one digit in string
(?=.*[a-z]) - checks for at-least one lowercase in string
(?=.*[A-Z]) - checks for at-least one uppercase in string
(?=.*[_#.#&+-]) - checks for at-least one special character in string
{8,} - checks the string length for a minimum of 8 characters
Click here to test it out at Regex 101.
Probably I couldn't convey the issue am trying to sort it out. My case is
If All Numbers--Error
If All Special Characters--Error
If only Numbers + special Characters--Error
Remaining all should be flaged as non error. For the special characters, I get a specific list to let them in. I tried & tried finally Below Regex seems working. Any Optimizations?
\d*([a-zA-Z,/().][a-zA-Z][,/().])[a-zA-Z\d\s]*$
I'm try to build regex pattern which requires the string to contain multicase letters together, but there's no success.
Here's what I have, but it doesn't work:
(?=[A-Z]+)(?=[a-z]+)(?=[0-9]+)
In other words, the string should to match only if it contains uppercase and lowercase and digits in any order like that:
MyPass777 <-- match
Mypass777 <-- match
MyPass <-- no match
mypass777 <-- no match
So, how to let this work?
Your positive lookaheads must also use .* before your conditions to allow for any arbitrary number of character before letter or numbers:
\b(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])[A-Za-z0-9]+\b
RegEx Demo
Also note use of \b (word boundary) on either side of your regex to make sure to match complete words only.
If you want a yes/no test, then use alternation.
Require something that has a upper and eventually a lower OR something that has a lower and eventually a upper.
With spaces added for clarity
(?: [a-z].*[A-Z] | [A-Z].*[a-z] )
With a third requirement, numbers, it gets combinatorially more expensive.
You're better off testing in three phases. Does this have a uppercase? If not, fail. Does it have a lowercase? If not, fail. Does it have a number? If not, fail. Else, it's okay.
Use separate regexes instead of single regex to gain additional benefits.
With this approach, you do not limit user to enter uppercase+lowercase+digits, but if they use for example uppercase+lowercase+punctation, the password will be considered equally good.
Test 4 cases:
[A-Z]
[a-z]
[0-9]
[\!+\-*##$%\^&*[\]{}:";'<>?,./] ' or refer to Unicode character class P (punctuation) instead
Now count matching cases.
1-2 cases: weak password.
3 cases: good password.
4 cases: strong password.
This pattern does forward lookahead and requires that the next character be an uppercase letter, a lowercase letter, and a digit at the same time. It never matches.
You want something like
(?=\w*[A-Z])(?=\w*[a-z])(?=\w*[0-9])(\w+\b)
At least, that's my best understanding of your problem: You want a string of alphanumeric characters that contains at least one uppercase letter, at least one lowercase letter, and at least one digit.
I'm working on some legacy code, found a regex and was hoping someone could dissect it for me:
((?=.*[^a-zA-Z])(?=.*[a-z])(?=.*[A-Z]).{8,})
I know what it's doing, but not entirely sure how it's doing it. What exactly is the "." doing after the conditions?
I'm trying to add some code to generate this regex dynamically. That is, if a bunch of configuration says the password can do anything, I want to return a regex that will return true for ALL CALLS OF .matches() in groovy. So far I've just done string formatting, so the regex created is "(.)", however this is returning false when called with matches().
Thanks for the help!
Here's the explanation broken down for you and a couple example strings.
http://regex101.com/r/oZ6dK4
the tl;dr here, is using the lookahead assertions, it's requiring that at least somewhere in the string you have:
a lowercase letter [a-z]
an uppercase letter [A-Z]
a NON-letter [^a-zA-Z]
and that it must be at least 8 characters {8,}
This looks like a password requirements validator to me.
Strings it will match:
asdfsdklj2-3049-09AS
09809LK2JL23Lsdf
Strings it won't match:
asdfsdf
2398-02934
23Abs
As for your question about the dot (.): it's not a period, it's a regex special character that matches anything except newline. (In the regex101 explanation you can see it states .{8,} matches any character (except newline)). In this case, the reason ~"(.)".matches() returns false, is because it requires a minimum of 8 characters in order to validate.
This checks to ensure the password contains a non-alphabetic character, that it contains at least one lowercase and uppercase alphabetic character, and finally that the entire string is at least 8 characters in length. The latter portion is where the . comes in – matching the entire string and ensuring it's 8+ characters.
(?=.*[^a-zA-Z) - this checks that there is a non-alpha character somewhere in the password
(?=.*[a-z]) - this checks that there is a lower case character in the password somewhere
(?=.*[A-Z]) - this checks that there is an upper case character in the password somewhere
.{8,} - this checks that there are at least 8 characters.
?= is the positive lookahead. The regex in this bracket must be matched and it must occur after this spot in the input.
Check rexegg.com for all kinds of useful regex information and a quick reference that's very useful.
I am trying to do do following match using regex.
The input characters should be capital letters starting from 2-10 characters.
If it's 2 characters then allow only those 2 characters which does not contain A,E,I,O,U either at first place or second place.
I tried:
[B-DF-HJ-NP-TV-XZ]{2,10}
It works well, but I am not too sure if this is the right and most efficient way to do regex here.
All credit to Jerry, for his answer:
^(?:(?![AEIOU])[A-Z]{2}|[A-Z]{3,10})$
Explanation:
^ = "start of string", and $ = "end of string". This is useful for preventing false matches (e.g. a 10-character match from an 11 character input, or "MR" matching in "AMRXYZ").
(?![AEIOU]) is a negative look-ahead for the characters A,E,I,O and U - i.e. the regex will not match if the text contains a vowel. This is only applied to the first half of the conditional "OR" (|) regex, so vowels are still allowed in longer matches.
The rest is fairly obvious, based on what you've already demonstrated an understanding about regex in your question above.