Can anyone tell me what “?=” does when using regex?
Here is an example of the code fragment I am trying to decipher:
password.matches("(?=.*\\d.*\\d.*)^[\\w]{8}.*$");
Thanks.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s a positive lookahead. In that particular expression, it is saying that your password must have at least two digits (
\d).Also note that a lookahead doesn’t consume input, it is merely an assertion.
For example, in your regex, the lookahead part (
(?=.*\\d.*\\d.*)) asserts that yourpasswordcontains at least two digits, and the rest of the expression consumes the entire string, and tries to match at least 8 word characters (i.e.,[a-zA-Z_0-9]) at the beginning of the string.