Python's 'match' line in _sre.SRE_Matc

2019-03-04 06:37发布

问题:

Using python 3:

In [275]: blah = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffg"

In [276]: pat = re.compile("([a-z]{2,9999})")

In [277]: data = re.search(pat,blah)

In [278]: data
Out[278]: <_sre.SRE_Match object; span=(0, 125), match='fffffffffffffffffffffffffffffffffffffffffffffffff>

Is it possible for match='' to print out the whole string? i.e all the way to the final 'g'?

回答1:

No, it is not possible. That length is hard coded in the format string in the match object's repr method and is not designed to capture the full length of the matched string.

Except you compile your own build of CPython (or whatever Python flavour you're at) with the precision of the representation of the match object in match_repr modified. Default precision is 50:

result = PyUnicode_FromFormat(
        "<%s object; span=(%d, %d), match=%.50R>",
        Py_TYPE(self)->tp_name,
        self->mark[0], self->mark[1], group0);

As others have suggested, simply use the group method of the match object to access the full matching string(s).



回答2:

With this one you don't constrain yourself with any upper limit of repetitions:

import re

blah = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffg"

r = re.compile(r'[a-z](.)\1{2,}') # no upper limit
try:
    re.search(r, blah).group()
except AttributeError:
    pass  # whatever you want to do when there is no match


回答3:

You are probably looking for the .group ...

import re
blah = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffg"
pat = re.compile("([a-z]{2,99})")
data = re.search(pat,blah)
if data:
    data.group(0) # returns 'fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'
else:
    pass # do something when the string was not found!

See: https://docs.python.org/3.5/library/re.html#re.match.group

and

Why won't re.groups() give me anything for my one correctly-matched group?