176
Chapter 7
The dot-star uses
greedy mode: It will always try to match as much text as
possible. To match any and all text in a
non-greedy fashion, use the dot, star,
and question mark (
.*?
). Like with braces, the question mark tells Python
to match in a non-greedy way.
Enter the following into the interactive shell to see the difference
between the greedy and non-greedy versions:
>>>
nongreedyRegex = re.compile(r'<.*?>')
>>>
mo = nongreedyRegex.search(' for dinner.>')
>>>
mo.group()
'
'
>>> greedyRegex = re.compile(r'<.*>')
>>> mo = greedyRegex.search(' for dinner.>')
>>> mo.group()
' for dinner.>'
Both regexes roughly translate to “Match an opening angle bracket,
followed by anything, followed by a closing angle bracket.” But the string
' for dinner.>'
has two possible matches for the closing angle
bracket. In the non-greedy version of the regex, Python matches the short-
est possible string:
''
. In the greedy version, Python matches
the longest possible string:
' for dinner.>'
.
Dostları ilə paylaş: