繁体   English   中英

正则表达式替换字符串之间[]

[英]Regex Replace String Between []

我有这段代码用于替换()之间的内容

 Regex yourRegex = new Regex(@"\(([^\)]+)\)");

            //Example strings
           string rep1 yourRegex.Replace("This is a (variable) string.", ""); //yields "This is a string."

结果是"This is a string."

但是我有这个字符串“这是一个[变量]字符串”。 现在如何将Regex用于相同的替换方法?

采用

Regex yourRegex = new Regex(@"\(.*\)|\[.*\]");

我的版本仅使用匹配项,而您的版本使用的组贵一些,对于您要完成的工作来说则不必要。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM