2017-02-23 31 views
0

我想我可能已經發現了Python的正則表達式的錯誤,或者我犯了一個錯誤?Python正則表達式匹配對象屬性錯誤?

import regex 

... 

iters = regex.finditer("Teams? [^u]*? rejected",file) 
for Result in iters: 
    Beginning = Result.span()[0] 
    End = Result.span()[1] 
    Text = Result.match() 

運行上面的代碼給出了以下結果/錯誤。它清楚地輸出帶匹配屬性的regex.Match對象,然後給出該對象沒有屬性匹配的錯誤。

<regex.Match object; span=(7684, 7708), match='Teams 1, 2 and 7 are rejected'> 
Traceback (most recent call last): 
File "b.py", line 72, in <module> 
Text = Result.match() 
AttributeError: '_regex.Match' object has no attribute 'match' 

我寫了這個代碼前一段時間在不同的計算機上,它的工作。現在在我的新電腦上出現這個錯誤。不知道我的以前版本的正則表達式是什麼,這是我目前的版本。

>>pip show regex 
Name: regex 
Version: 2017.2.8 
Summary: Alternative regular expression module, to replace re. 
Home-page: https://bitbucket.org/mrabarnett/mrab-regex 
Author: Matthew Barnett 
Author-email: [email protected] 
License: Python Software Foundation License 
+0

99.9%的時間,如果你想知道它是流行軟件中的錯誤還是你犯了錯誤,那就是後者。 – Barmar

+0

我只是說,因爲代碼在以前的版本上工作得很好,現在它沒有。 – projectgonewrong

+0

標準Python正則表達式模塊的名稱是're',而不是'regex'。 – Barmar

回答

3

regex應該是與re兼容。 finditer迭代器返回的Match對象中沒有match屬性。匹配整個正則表達式的方法是Result.group(0)或簡單地Result.group()。可以簡化爲Result.start()Result.end()

re.Match對象這裏

我不知道爲什麼它工作之前的文檔。也許老版本的regex模塊暴露了一個內部屬性,並且這個問題已經修復。