带括号的正则表达式

yon*_*ano 0 regex

我正在尝试从字符串中删除以下内容:

细绳:

Snowden (left), whose whereabouts remain unknown, made the extraordinary claim as his father, Lon (right), told US television he intended to travel
Run Code Online (Sandbox Code Playgroud)

我正在使用以下正则表达式:([(].*[)]),但它匹配:

(left), whose whereabouts remain unknown, made the extraordinary claim as his father, Lon (right)
Run Code Online (Sandbox Code Playgroud)

这是有道理的,但不是我想要的。

我能做什么来解决这个问题?这与贪婪或懒惰有关系吗?

编辑:

我正在使用Python:

paren = re.findall(ur'([(\u0028][^)\u0029]*[)\u0029])', text, re.UNICODE)

        if paren is not None:
                text = re.sub(s, '', text)
Run Code Online (Sandbox Code Playgroud)

这将导致以下输出:

 Snowden (), whose whereabouts remain unknown, made the extraordinary claim as his father, Lon (), told US television he intended to travel
Run Code Online (Sandbox Code Playgroud)

但是,当我打印 paren.group(0) 时,我得到“(left)”,这意味着包含括号,这是为什么?

谢谢。

abi*_*ssu 5

使用否定:([(][^)]*[)]). 这将匹配开头(,然后是任意数量的不是结尾的字符),然后是结尾)

您可以通过这种方式否定任何字符或字符集。要匹配文字^插入符号,您可以将其放在[]字符集之外或将其放在第一个字符之后的任何位置,如下所示:[a^bc]。阅读您正在使用的正则表达式语言的规则以准确了解什么是可能的以及正确的语法总是一个好主意。

贪婪或懒惰是一条规则,在所有正则表达式实现中可能不会以相同的方式(如果有的话)实现。最好明确地说出您想要查找的内容,而不是依赖于难以理解和调试的规则(有时)。