_sre.SRE_Match输出中的Python'match'行?可以显示完整的比赛吗? [英] Python's 'match' line in _sre.SRE_Match output? Can it show the full match?

查看:174
本文介绍了_sre.SRE_Match输出中的Python'match'行?可以显示完整的比赛吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用python 3:

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>

match=''是否可以打印出整个字符串?即一直到最后的'g'?

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

推荐答案

否,这是不可能的.该长度是硬编码在match对象的repr方法中的格式字符串中的,并非旨在捕获匹配字符串的完整长度.

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

正如其他人所建议的那样,只需使用 group 匹配对象的方法来访问完整的匹配字符串.

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

这篇关于_sre.SRE_Match输出中的Python'match'行?可以显示完整的比赛吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆