[英]Python regex: Multiple matches in one line (using findall())
我在文本中尋找這些“標簽”: {td="var1"}var2{/t}
或{td="varA"}varB{/t}
可以有更多屬性,只有“d”是必需的: {td="var1" foo="bar"}var2{/t}
我的問題是 - 如果一行上有更多標簽,則返回一個結果,而不是全部結果。 返回的內容(來自下面的測試字符串):( (u'single1', u'Required item3')
我希望得到的回報是:(你的(u'single1', u'required1') (u'single2', u'Required item2') (u'single3', u'Required item3')
我(u'single1', u'required1') (u'single2', u'Required item2') (u'single3', u'Required item3')
。 它每行使用一個標記,但每行不使用更多標記。
# -*- coding: UTF-8 -*-
import re
test_string = u'''
<span><img src="img/ico/required.png" class="icon" alt="{t d="single1"}required1{/t}" title="{t d="single2"}Required item2{/t}" /> {t d="single3"}Required item3{/t}</span>
'''
re_pattern = '''
\{t[ ]{1} # start tag name
d=" # "d" attribute
([a-zA-Z0-9]*) # "d" attribute content
".*\} # end of "d" attribute
(.+) # tag content
\{/t\} # end tag
'''
rec_pattern = re.compile(re_pattern, re.VERBOSE)
res = rec_pattern.findall(test_string)
if res is not None:
for item in res:
print item
你的通配符很貪婪。 將它們從.*
更改為.*?
所以他們不會貪婪:
re_pattern = '''
\{t[ ]{1} # start tag name
d=" # "d" attribute
([a-zA-Z0-9]*) # "d" attribute content
".*?\} # end of "d" attribute
(.+?) # tag content
\{/t\} # end tag
'''
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.