I need to write a python function that takes a string, and uses REGEX to check if the string contains:
Returns true if these exist and false otherwise. I'm good with the function, however, I'm having trouble with the regular expression.
What I have so far is: [A-Z]+\d{2,}[!@#\$&\*-_\.]{2}
I know this doesn't work, I'm really confused since I'm new to regex.
Thanks for your help!
You can use
^(?=[^A-Z\r\n]*[A-Z])(?=[^\d\r\n]*\d[^\d\r\n]*\d)(?=.{6,8}$)[A-Z\d]*[!@#$&*_.-][A-Z\d]*[!@#$&*_.-][A-Z\d]*$
Note to escape the \-
in the character class or place it at the start or end. Else it would denote a range.
Explanation
^
Start of string(?=[^A-Z\r\n]*[A-Z])
Positive lookahead, assert a char A-Z(?=[^\d\r\n]*\d[^\d\r\n]*\d)
Positive lookahead, assert 2 digits(?=.{6,8}$)
Positive lookahead, assert 6 - 8 characters in total[A-Z\d]*[!@#$&*_.-][A-Z\d]*[!@#$&*_.-][A-Z\d]*
Match 2 "special" characters$
End of string (Or use \Z
if there can no newline following)
This is perfect! Only problem is it doesn't match if I have any lowercase characters in the string. Like for example the string "A00!$a" is supposed to match. Do you know what I can do to fix this?
@SegmentationFault You can add a-z to the matching character class, see regex101.com/r/5zbejo/1
Perfect that works thanks!