Positive and Negative Lookahead \w metacharacter confusion

Condition given in question : Use lookaheads in the pwRegex to match passwords that are greater than 5 characters long, do not begin with numbers, and have two consecutive digits.

Why ^ (?=\w{6})(?=\D+\d{2}) works as solution?

since \w means [A-Za-z0-9_] this should evalute to true if the passwords starts from number, which violates the given condition

But it passes all the test cases. Please correct me if i got it wrong

To receive a better feedback please post the challenge link^^
Anyway, i think the correct statement is:

evalute to true if the passwords starts from number or a letter

It means that your regex will match all the password that doesn’t start with a number, \w is just broader than it could be i guess ^^

both lookaheads start looking at the same character, so the second look ahead says that the string can’t start with a number (with \D)

1 Like

What is logic of putting \w since we don,t want a password to start with number. Please magical girl can you please explain in detail with example or could u provide a me link to study more about it.


\w{6} this day it needs to have 6 alphanumeric characters at the beginning
and \D+\d{2} and this that it needs to have at the beginning at least 1 non number character and then after that 2 number characters
putting together the two it satisfy more or less the requirements of the challenge

1 Like

Yes 6 apha Numeric…does it mean it will always start with alpha then numeric or vice-versa is ture ?

it can’t start with a numeric value because the second look ahead is looking for a non-number value as first character

1 Like

Using two lookaheads means both have to match. That is why it is fine to use the \w{6} for one of them.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.