Python中re.findall的贪心和非贪心算法
Python中re.findall的贪心和非贪心算法:
import re s1='hjxxHelloxxrynxxPythonxxplk' f1=re.findall('xx.*xx',s1) print(f1) f2=re.findall('xx.*?xx',s1) print(f2) f3=re.findall('xx(.*)xx',s1) print(f3) f4=re.findall('xx(.*?)xx',s1) print(f4) s2='''hjxxHello xxrynxxPythonxxplk''' g1=re.findall('xx(.*?)xx',s2) print(g1) g2=re.findall('xx(.*?)xx',s2,re.S) print(g2)
输出为:
['xxHelloxxrynxxPythonxx']
['xxHelloxx', 'xxPythonxx']
['HelloxxrynxxPython']
['Hello', 'Python']
['ryn']
['Hello\n', 'Python']