4

我需要有关正则表达式模式的帮助。

我有这个字符串:

test = "/*testing 1*/Name: /*Testing 2*/ My Name" 

我需要/**contents **/从字符串中删除每个。

我正在使用正则表达式进行过滤,如下代码:

Regex rx = new Regex(@"/\*[^>]+\*/");
Template = rx.Replace(Template, match => { return String.Empty; });

但我得到的结果只是"My Name",预期结果是"Name: My Name"

4

1 回答 1

4

Change your regex to /\*[^>]+?\*/.

The ? after the + makes it non greedy so the regex stops at the first */ instead of the last. Be aware though that if you have nested /* ... */ blocks the regex will fail.

于 2013-10-23T00:00:43.153 回答