如何使用正则表达式替换括号中的字符串? [英] How can I replace a string in parentheses using a regex?

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

问题描述

我有一个字符串:

  HLN(旧标题新闻)

我想删除parens和parens中的所有内容,只留下:

  HLN 

我试图用正则表达式做到这一点,但是我的困难在于这种模式:

 (。+?)

当我使用它时,它总是给我一个 PatternSyntaxException 。我如何修复我的正则表达式?

解决方案

因为括号是regexp中的特殊字符,所以你需要转义它们以明确地匹配它们。 / p>

例如:

 \\(。+? \\)


I have a string:

HLN (Formerly Headline News)

I want to remove everything inside the parens and the parens themselves, leaving only:

HLN

I've tried to do this with a regex, but my difficulty is with this pattern:

"(.+?)"

When I use it, it always gives me a PatternSyntaxException. How can I fix my regex?

解决方案

Because parentheses are special characters in regexps you need to escape them to match them explicitly.

For example:

"\\(.+?\\)"

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

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