Is there any difference between (\w+)? and (\w*) in regex?
It seems the same, doesn’t it?
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.
(\w+)?and(\w*)both match the same (0..+inf word characters)However, there is a slight difference:
In the first case, if this part of the regex matches
"", the capturing group is absent. In the second case, it is empty. In some languages, the former manifests as anullwhile the latter should always be"".In Javascript, for example,
In PHP (
preg_match), in the former case, the corresponding key is simply absent in the matches array: http://3v4l.org/DB6p3#v430