正则表达式替换"令牌的多个实例"按以下格式:[[TokenName:TokenValue]]?

时间:2016-08-05 17:44:01

标签: c# .net regex

我正在寻找可以用空字符串替换[[Something:SomethingElse]]的任何实例的正则表达式。

2 个答案:

答案 0 :(得分:0)

试试这个正则表达式:\[\[.*?:.*?\]\]

string str = @"some string before pattern [[Something:SomethingElse]] between
               pattern [[doesnt matter:at all]] after pattern";
string result = Regex.Replace(str, @"\[\[.*?:.*?\]\]", "");

Outout:

some string before pattern  between pattern  after pattern

答案 1 :(得分:0)

使用Regex.Replace和LookArounds:

value = Regex.Replace(value, @"(?=\[\[)(.*?:.*?)(?<=\]\])", string.Empty);

测试用例:

string value = "89as89d98as98d[[lksdlkfajks03:34]],[[939203902:ksadfjlkaklsdf]]9390290390ks";

输出:

value = "89as89d98as98d,9390290390ks"