Regular Expression - Positive and Negative Lookahead

Hi all,

Newbie question for the lesson Regular Expressions: Positive and Negative Lookahead:

I need to use lookaheads in the pwRegex to match passwords that are greater than 5 characters long, and have two consecutive digits.

My question is why does the below work, where the second lookahead needs \w* which i thought was zero or more?
(?=\w{6,})(?=\w*\d\d)

I thought that (?=\w{6,})(?=\d\d) should work, i presume i am missing something fundamental and basic here.

If i were to have a sampleWord “abcdef12” it would fail (with my second look ahead) but pass with sampleWord “123456”

Thanks,

A positive lookahead asserts that the given subpattern can be matched here, without consuming characters.

(emphasis added)

In your second expression, the second lookahead will not match because it does not account for word characters.

1 Like