Python's 'match' line in _sre.SRE_Match output? Can it show the full match?

时光怂恿深爱的人放手 提交于 2020-01-11 14:12:00

问题


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?



来源:https://stackoverflow.com/questions/48675282/pythons-match-line-in-sre-sre-match-output-can-it-show-the-full-match

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!