2017-02-23 65 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模块暴露了一个内部属性,并且这个问题已经修复。