我需要搜索的字符串和替换%名字%
和%PolicyAmount%
中所有出现一个值从数据库中抽取。问题是名字的大小写不同而不同。这$ P $使用该与string.replace()
方法pvents我。我已经看到了关于这个问题的web页面提示
I need to search a string and replace all occurances of %FirstName%
and %PolicyAmount%
with a value pulled from a database. The problem is the capitalization of FirstName varies. That prevents me from using the String.Replace()
method. I've seen web pages on the subject that suggest
Regex.Replace(strInput, strToken, strReplaceWith, RegexOptions.IgnoreCase);
不过,对于当我尝试用 $ 1,0
替换%PolicyAmount%
某些原因,更换从未发生。我认为它是与美元符号是在正则表达式保留字符。
However for some reason when I try and replace %PolicyAmount%
with $0
, the replacement never takes place. I assume that it has something to do with the dollar sign being a reserved character in regex.
有另一种方法,我可以使用,不涉及消毒的输入,处理正则表达式的特殊字符?
Is there another method I can use that doesn't involve sanitizing the input to deal with regex special characters?
从MSDN $ 0 - 替代匹配的组号编号(十进制)的最后一个子
From MSDN $0 - "Substitutes the last substring matched by group number number (decimal)."
在.NET正EX pressions组0总是整场比赛。对于文字$需要
In .NET Regular expressions group 0 is always the entire match. For a literal $ you need to
string value = Regex.Replace("%PolicyAmount%", "%PolicyAmount%", @"$$0", RegexOptions.IgnoreCase);