2016-07-14 25 views
-1

我有沿的線條圖案:Python的正則表達式的錯誤,非gredy組意外的行爲

patterns=['file.html', '../file','../file.html', 'file/', 'file']

編號就像一個正則表達式匹配只能在這些場景文件。 rgx = re.compile(r"^ #begin line (\.\./)? #1st group non-greedy (.*)? #2nd group non-greedy (\.html$)? #3rd group non-greedy $ #pattern end", re.VERBOSE)

問題出在第二組。它不是非貪婪的,總是吞噬着.html

因此,對於m= rgx.match('file.html')輸出m.group(2)file.html,而不是所需的file

我在做一個明顯的錯誤,這裏有什麼問題?

任何幫助表示讚賞: 三江源

+0

你的代碼在哪裏? – melpomene

+0

您的正則表達式中沒有非貪婪組。 – melpomene

+1

如果你指出包含輸入和期望輸出的例子,你的問題會更清晰。 – xgord

回答

0

呀@melpomeme,感謝非貪婪澄清。移動了?緊跟*限定符之後,而不是在group()之前。現在它工作,謝謝