I wrote a regex that validates an input string. It must have a minimum length of 8 chars (composed by alphanumeric and punctuation chars) and it must have at least one digit and one alphabetic char. So I’ve come up with the regex:
^(?=.*[0-9])(?=.*[a-zA-Z])[a-zA-Z0-9-,._;:]{8,}$
Now I have to rewrite this regex in a language that doesn’t support lookahead, how should I rewrite that regex?
Valid inputs are:
1foo,bar
foo,bar1
1fooobar
foooobar1
fooo11bar
1234x567
a1234567
Invalid inputs:
fooo,bar
1234-567
.1234567
There are two approaches. One is to compose a single expression which handles all possible alternatives:
etc. This is a combinatoric nightmare, but it would work.
A much simpler approach is to validate the same string twice using two expressions:
and
EDIT: @briandfoy correctly points out that it will be more efficient to search for each required character separately:
and