Python正则表达式捕获组issu

2024-05-14 12:07:00 发布

您现在位置:Python中文网/ 问答频道 /正文

试图指定我的捕获组,但它总是捕获太多。你知道吗

生产线:

"This is something of [Interest: stuff]. blah blah blah"

正则表达式:

patt = re.compile('\[Interest:(.){1,100}\]')

什么是输出:

[Interest: stuff]

我想要的输出:

stuff

我怎样才能输出我想要捕获的内容,而不是整个模式?你知道吗

我也试过:

re.compile(r'\[Interest:(?P<interest>.+)\]')

输出:

stuff]. blah blah blah

我觉得我很接近。只需要弄清楚如何在regex到达


Tags: ofre内容is模式thissomethingregex
3条回答

regex的一个问题是:\[Interest:(.){1,100}\],即(.){1,100}允许1到100个.,但它将只捕获一个.,即最后一个.,因为()只包含.(指单个字符)。因此,捕获的将包含stufff。你知道吗

相反,\[Interest: (.{1,100})\]将返回stuff。你知道吗

至于输出是[Interest: stuff]。。这是一个分组问题。
试试iCodez的代码HERE

>>> import re
>>> string = "This is something of [Interest: stuff]. blah blah blah"
>>> re.search("\[Interest:\s([^\]]*?)\]", string).group(1)

它打印stuff。你知道吗

.group(0)替换.group(1),然后打印[Interest: stuff]。你知道吗

使用lazy方法从索引1中获取匹配的组。你知道吗

\[Interest: (.*?)\]

DEMO

示例代码:

import re
p = re.compile(ur'\[Interest: (.*?)\]', re.IGNORECASE)
test_str = u"This is something of [Interest: stuff]. blah blah blah"

re.match(p, test_str)

.字符匹配除换行符以外的所有字符,包括]。因此,(.){1,100}告诉Python获取它所能获取的长度为100个字符的所有内容。这包括字符串的结尾。你知道吗

相反,我将使用以下模式:

\[Interest:\s([^\]]*)\]

演示:

>>> import re
>>> string = "This is something of [Interest: stuff]. blah blah blah"
>>> re.search("\[Interest:\s([^\]]*)\]", string).group(1)
'stuff'
>>>

下面是它的匹配说明:

\[         # [
Interest:  # Interest:
\s         # A space
(          # The start of a capture group
[^\]]*     # Zero or more characters that are not ]
)          # The close of the capture group
\]         # ]

有关详细信息,请参阅Regular Expression Syntax。你知道吗

相关问题 更多 >

    热门问题