以下是我的代码
Dim ATT As String = "Hi Sample $1$ Text"
Dim newText As String = "<ss>Hello How Are you</ss>"
newText = Regex.Replace(newText, "<ss>(.*?)</ss>", ATT, RegexOptions.IgnoreCase)
以上代码将ATT变量中的字符串内容(“$ 1 $”)视为Regex值并将其替换为newText变量,如何克服这一点,请指导,
有没有像我们在下面提到的perl中可用的东西来克服这个问题,
$_=~ s/<ss>(.*?)</ss>/\Q$ATT\E/g;
答案 0 :(得分:2)
您可以在替换字符串中escape $
个字符:
Dim ATT As String = "Hi Sample $1$ Text"
ATT = ATT.Replace("$", "$$")
Dim newText As String = "<ss>Hello How Are you</ss>"
newText = Regex.Replace(newText, "<ss>(.*?)</ss>", ATT, RegexOptions.IgnoreCase)
或者使用lambda进行替换:
Dim ATT As String = "Hi Sample $1$ Text"
Dim newText As String = "<ss>Hello How Are you</ss>"
newText = Regex.Replace(newText, "<ss>(.*?)</ss>", Function(m) ATT, RegexOptions.IgnoreCase)