例子如下:
a = 'Peter d,, 13tsddgjlsv >>bgeghg "desfegeivm,x,wb.rhwrohjow" dddeuvb "dwegjosnngweibmbe,,benbie" hello world '
输出的结果如下(字符串):
'Peter d,, 13tsddgjlsv >>bgeghg
dddeuvb
hello world'
三叔2016-11-11 13:30:28
import re a ='Peter d,, 13tsddgjlsv >>bgeghg \n"desfegeivm,x,wb.rhwrohjow" dddeuvb \n"dwegjosnngweibmbe,,benbie" \nhello world ' print a print '&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n' match_list = re.findall(r'([^"]*)\".*\"([^"]*)', a) #返回一个数组,每个数组元素由一个元组构成 print 'match_list:', match_list, '\n' print 'result: ' for x in match_list: #遍历match_list匹配到的结果 for i in x: if i: #如果不为空,则print print i.strip()
输出如下:
Peter d,, 13tsddgjlsv >>bgeghg
"desfegeivm,x,wb.rhwrohjow" dddeuvb
"dwegjosnngweibmbe,,benbie"
hello world
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
match_list: [('Peter d,, 13tsddgjlsv >>bgeghg \n', ' dddeuvb \n'), ('', ' \nhello world ')]
result:
Peter d,, 13tsddgjlsv >>bgeghg
dddeuvb
hello world