Regex替换c#中的特定单词

本文关键字:单词 替换 Regex | 更新日期: 2023-09-27 18:02:25

我有一个字符串,如"请....{@@url} something{@@test} something…有些{@@url} ....",想通过regex在c#

中精确地替换字符串{@@url}和{@@test}

请帮助我获得正则表达式模式

提前感谢!

Regex替换c#中的特定单词

Regex获取大括号之间的字符串"{I want what's between the大括号}"

https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex (v = vs.110) . aspx

比如

string pattern = @"/{(.*?)}/";
string input = "Please .... {@@url} something{@@test}somethinng ... some{@@url} ....";
Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
MatchCollection matches = rgx.Matches(input);
if (matches.Count > 0)
{
  Console.WriteLine("{0} ({1} matches):", input, matches.Count);
  foreach (Match match in matches)
     input = input.replace(match.value, "");
}