场景分析
使用python正则表达式提取某段中多行内容,例如:
‘’’
aaaa bbbb cccc
xx abcdefg
abcdefg
abc yy
abc abde adf
dadfeljgslka lkdsjgls
xx adgei ,
fdasd yy
adg asfgk ksdg
adsa xx
dga dgl yy
alkdg
‘’’
提取被xx
和yy
包围的字段
不使用compile
with open('./filename.txt', 'r') as f:
content = f.read()
import re
re_str = r'xx.+yy' # the resp will not include 'xx' and 'yy' if you use the 'xx(.+)yy'
resp = re.findall(re_str, content, re.S)
print resp
使用compile
with open('./filename.txt', 'r') as f:
content = f.read()
import re
re_str = r'xx.+yy' # the resp will not include 'xx' and 'yy' if you use the 'xx(.+)yy'
comp = re.compile(re_str, re.S)
resp = comp.findall(content)
print resp