What does this Python regex match?
.*?[^\\]\n
I’m confused about why the . is followed by both * and ?.
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.
*means “match the previous element as many times as possible (zero or more times)”.*?means “match the previous element as few times as possible (zero or more times)”.The other answers already address this, but what they don’t bring up is how it changes the regex, well if the
re.DOTALLflag is provided it makes a huge difference, because.will match line break characters with that enabled. So.*[^\\]\nwould match from the beginning of the string all the way to the last newline character that is not preceeded by a backslash (so several lines would match).If the
re.DOTALLflag is not provided, the difference is more subtle,[^\\]will match everything other than backslash, including line break characters. Consider the following example:So the purpose of this regex is to find non-empty lines that don’t end with a backslash, but if you use
.*instead of.*?you will match an extra\nif you have an empty line following a non-empty line.This happens because
.*?will only matchfo,[^\\]will match the secondo, and the the\nmatches at the end of the first line. However the.*will matchfoo, the[^\\]will match the\nto end the first line, and the next\nwill match because the second line is blank.