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']

posted @ 2018-05-31 09:07  永远怀着一颗学习的心  阅读(406)  评论(0编辑  收藏  举报