使用正则表达式删除括号Java中的所有内容 [英] Remove everything in parentheses java using regex

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

问题描述

我使用以下正则表达式尝试删除括号以及其中的所有内容,这些字符串称为 name .

I've used the following regex to try to remove parentheses and everything within them in a string called name.

name.replaceAll("\\(.*\\)", "");

由于某种原因,这使名称保持不变.我在做什么错了?

For some reason, this is leaving name unchanged. What am I doing wrong?

推荐答案

字符串是不可变的.您必须这样做:

Strings are immutable. You have to do this:

name = name.replaceAll("\\(.*\\)", "");

而且,由于.* 是贪婪的,它将杀死尽可能多的人.因此(abc)something(def)" 将变成" .

Also, since the .* is greedy, it will kill as much as it can. So "(abc)something(def)" will be turned into "".

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

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