All pastes #552318 Raw Edit

python string regex

public text v1 · immutable
·published 2007-06-09 07:07 UTC
rendered paste body
Trying to match a C-like string, so that inside quotes, only non-quotes or backslash-escaped quotes are allowed:

>>> p = re.compile(r'"([^"]|\\")*"')
>>> p.match(r'"quote: \" test"').group()
'"quote: \\"'

But how come that wasn't greedy, and this is?
                                  V=== All I added was this dollar sign...
>>> p = re.compile(r'"([^"]|\\")*"$')
>>> p.match(r'"quote: \" test"').group()
'"quote: \\" test"'