-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
。
我在做一個明顯的錯誤,這裏有什麼問題?
任何幫助表示讚賞: 三江源
你的代碼在哪裏? – melpomene
您的正則表達式中沒有非貪婪組。 – melpomene
如果你指出包含輸入和期望輸出的例子,你的問題會更清晰。 – xgord