projectgonewrong
projectgonewrong

Reputation: 27

Python Regex Match Object Attribute Bug?

I think I may have found a bug in py regex, or have I made an error?

import regex

...

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

Running the above code gives the following result/error. It clearly outputs the regex.Match object with the match attribute then gives an error that the object has no attribute 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'

I wrote this code awhile ago on a different computer and it worked. Now on my new computer it gives this error. Not sure what my previous version of regex was, this is my current version.

>>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

Upvotes: 0

Views: 5051

Answers (1)

Barmar
Barmar

Reputation: 781721

regex is supposed to be compatible with re. There's no match property in the Match object returned by the finditer iterator. The way to get the match for the whole regexp is with Result.group(0) or simply Result.group().

Also, Result.span()[0] and Result.span()[1] can be simplified to Result.start() and Result.end().

See the documentation of re.Match objects here

I don't know why it worked before. Maybe an older version of the regex module was exposing an internal property, and this was fixed.

Upvotes: 8

Related Questions