regEx包装字符串不区分大小写

Tre*_*vor 0 c# regex

当谈到正则表达式时,我是一个完整的新手,并且想知道是否有人可以帮助我.我不确定在这里使用regEx是否正确,所以如果你有更好的想法,请随时加入.(我将通过许多字符串循环).

基本上,我想在字符串上查找/替换,用{}包装匹配并保留字符串的原始大小写.

例:

Source: "The CAT sat on the mat."    
Find/Replace: "cat"    
Result: "The {CAT} sat on the mat."
Run Code Online (Sandbox Code Playgroud)

我希望find/replace只能在第一次出现时工作,我还需要知道find/replace是否确实匹配.

我希望我已经清楚地解释了一些事情.

谢谢.

It'*_*ie. 5

Regex theRegex = 
    new Regex("(" + Regex.Escape(FindReplace) + ")", RegexOptions.IgnoreCase);
theRegex.Replace(Source, "{$1}", 1);
Run Code Online (Sandbox Code Playgroud)

如果你想要字边界容差:

 Regex theRegex = 
     (@"([\W_])(" + Regex.Escape(FindReplace) + @")([\W_])", RegexOptions.IgnoreCase)
 theRegex.Replace(str, "$1{$2}$3", 1)
Run Code Online (Sandbox Code Playgroud)