如何使用正则表达式删除括号内的文本? [英] How can I remove text within parentheses with a regex?

查看:158
本文介绍了如何使用正则表达式删除括号内的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

这篇关于如何使用正则表达式删除括号内的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆