如何使用正则表达式删除括号内的文本?

问题描述:

我正在尝试处理一堆文件,然后我需要进行修改以删除文件名中的无关信息;值得注意的是,我正在尝试删除括号内的文本.例如:

I'm trying to handle a bunch of files, and I need to alter then to remove extraneous information in the filenames; notably, I'm trying to remove text inside parentheses. For example:

filename = "Example_file_(extra_descriptor).ext"

我想对一整堆文件进行正则表达式,其中括号表达式可能在中间或结尾,并且长度可变.

and I want to regex a whole bunch of files where the parenthetical expression might be in the middle or at the end, and of variable length.

正则表达式是什么样子?最好使用Perl或Python语法.

What would the regex look like? Perl or Python syntax would be preferred.

s/\([^)]*\)//

因此,在Python中,您可以这样做:

So in Python, you'd do:

re.sub(r'\([^)]*\)', '', filename)