如何使用c#正则表达式替换圆括号内的子字符串

时间:2014-11-04 07:12:43

标签: c# regex

假设我有一个这样的原始字符串:

Whatever here is (this is what i want to be replaced) Whatever here is

我想替换

this is what i want to be replaced

'this is what i want to be replaced'

我希望结果是:

Whatever here is ('this is what i want to be replaced') Whatever here is

我应该使用正则表达式来使其工作?

正则表达式模式总是让我感到困惑。

1 个答案:

答案 0 :(得分:2)

您搜索此正则表达式:

\(([^)]*)\)

并替换为:

('$1')

RegEx Demo

代码:您可以使用Regex.Replace方法:

string repl = Regex.Replace(input, @"\(([^)]*)\)", "$1");