Search code examples
pythonregexnon-greedy

Non-greedy mode in re.search() does not match to end of string


I'm trying to parse values of a cookie like this:

import re
m = re.search("(.*?)=(.*?); path=(.*?); domain=(.*?)", "name=value1; path=/; domain=my.domain.com")
print (m.group(0))

Result I get is like this:

name=value1; path=/; domain=

My question is: why does it not match at the last non-greedy position? Expected result would be:

name=value1; path=/; domain=my.domain.com

Of course, I could change to greedy mode or use an end of line character ($) but I'd like to understand why it's not working like I expected it to work :)


Solution

  • Non-greedy means it will match as little as it can while still allowing the entire match to succeed. * means "zero or more". So the least it can match is zero. So it matches zero and the match succeeds.

    The other occurrences of .*? in your regex cannot match zero, because then the entire regex will fail to match.