I have an input string which is considered valid only if it contains:
- At least one character in [a-z]
- At least one integer in [0-9], and
- At least one character in [A-Z]
There is no constraint on the order of occurrence of any of the above. How can I write a single regular expression that validates my input string ?
Try this
See it here online on Regexr
The
^and$are anchors which bind the pattern to the start and the end of the string.The
(?=...)are lookahead assertions. they check if the pattern after the=is ahead but they don’t match it. So to match something there needs to be a real pattern also. Here it is the.*at the end.The
.*would match the empty string also, but as soon as one of the lookaheads fail, the complete expression will fail.For those who are concerned about the readability and maintainability, use the
re.Xmodifier to allow pretty and commented regexes: