Re.compile 搜索未显示完整字符串

Re.compile search doesn't show the full string

在文本中我想查找文本是否包含以下字符串:

"http://p.thisistheurl.com/v/" 之后直到 "jpg".

所以这是我写的 python 代码:

asdf = 'http://p.thisistheurl.com/v/adzl25/4321567/543276123/865.jpg'

regex = re.compile(r'http://p.thisistheurl.com/v/(.)*jpg')

regex.search(asdf)

<_sre.SRE_Match object; span=(0, 60), match='http://p.thisistheurl.com/v/adzl25/4321567/543276'>

如您所见,结果并未显示带有 "jpg" 的整个字符串。为什么它不起作用?

我认为不能保证match=之后显示的字符实际上是匹配到的字符串的完整内容。它可能在 50 个字符左右后就被切断了。

再看cpython's implementation of SRE_Match.__repr__,确实是这样:右边的50R就是确凿的证据。

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

如果您访问实际匹配的字符串,而不是从匹配对象的打印表示中检查它,它会一直到 jpg:

>>> import re
>>> asdf = 'http://p.thisistheurl.com/v/adzl25/4321567/543276123/865.jpg'
>>> regex = re.compile(r'http://p.thisistheurl.com/v/(.)*jpg')
>>> print(regex.search(asdf).group(0))
http://p.thisistheurl.com/v/adzl25/4321567/543276123/865.jpg