Python正则表达式只适用于子字符串匹配,而不适用于整个字符串

2024-04-26 19:09:41 发布

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

我正在试图删除所有括号和插入的文本。我在用正则表达式

re.sub(r'\(.*\) | \[.*\]', '', text)

这适用于以下情况:

import re
text = 'the (quick) brown fox jumps over the [lazy] dog'
print re.sub(r'\(.*\) | \[.*\]', '', text)

> the brown fox jumps over the dog

text = '(the quick) brown fox jumps over the [lazy] dog'
print re.sub(r'\(.*\) | \[.*\]', '', text)

> brown fox jumps over the dog

但当整个字符串与正则表达式匹配时,它就失败了

text = '[the quick brown fox jumps over the lazy dog]'
print re.sub(r'\(.*\) | \[.*\]', '', text)

> [the quick brown fox jumps over the lazy dog]

> # This should be '' (the empty string) #

我哪里出错了?你知道吗


Tags: thetext文本importre情况quicklazy
3条回答

您有一个它试图匹配的额外空间:)

尝试:

re.sub(r'\(.*\)|\[.*\]', '', text)

当regex做这种奇怪的事情时,一个测试的好地方是here。这是一个很好的互动方式,看看哪里出了问题。例如,在你的例子中,它不匹配“(速度)”而是匹配“(速度)”,只要我在它后面加一个空格。你知道吗

注意:

正如我在评论中提到的,请注意,如果文本中有一个随机的“)”符号,贪婪匹配可能会做一些意想不到的事情,它可能只是一个独立的符号。考虑一下不情愿的匹配:

re.sub(r'\(.*?\)|\[.*?\]', '', text)

这将导致:

This is a (small) sample text with a ) symbol" ===> "This is a sample text with a ) symbol"

鉴于您目前将提供:

This is a (small) sample text with a ) symbol" ===> "This is a symbol"
import re
text = '''[the quick brown fox jumps over the lazy dog]
the (quick) brown fox jumps over the [lazy] dog
(the quick) brown fox jumps over the [lazy] dog'''
print (re.sub(r'[(\[].+?[)\]]', '', text))

输出:

the  brown fox jumps over the  dog
 brown fox jumps over the  dog

您在regex上有额外的空间,只需要删除|前后的空间

re.sub(r'\(.*\)|\[.*\]', '', text)

或者使它们成为可选的匹配项以匹配现有的输出

re.sub(r'\(.*\)\s?|\s?\[.*\]', '', text)

相关问题 更多 >