Python3

Python regex greedy VS non-greed

2019-01-07  本文已影响11人  JaedenKil
import re


a = "100100000001"
m = re.search(".+1", a)
print(m.group(0), '\n')
n = re.search(".+?1", a)
print(n.group(0))
100100000001 

1001

?, +?, ??
The '*', '+', and '?' qualifiers are all greedy; they match as much text as possible. Sometimes this behaviour isn’t desired; if the RE <.
> is matched against '<a> b <c>', it will match the entire string, and not just '<a>'. Adding ? after the qualifier makes it perform the match in non-greedy or minimal fashion; as few characters as possible will be matched. Using the RE <.*?> will match only '<a>'.
Refer to here.

上一篇下一篇

猜你喜欢

热点阅读