我使用Regex替换如下:
string test = "[abc] Sends the employee mail. [twist] Sends the employee mail.";
test = Regex.Replace(test, "[twist]", "hello");其结果是:
test = "[abc] Sendhello hellohe employee mahellol. [hellohellohellohellohello] Sendhello hellohe employee mahellol.",因为它应该用hello.代替扭弦。
这里出了什么问题。
发布于 2013-08-09 08:49:25
您必须转义括号(至少是开头的那个)。变化
"[twist]"至
"\\[twist\\]"或者,使用逐字文字来避免双重\,
@"\[twist\]"[twist]被解释为任何t、w、i、s或t字符,其中任何字符都被hello字符串替换。
发布于 2013-08-09 08:58:43
Regex.Replace(测试,“[扭曲]”,“你好”)
发布于 2013-08-09 09:15:04
作为额外的有用信息,您可以将[转义为[[],因此
"[[]twist]"或者你可以
"\\x5btwist]"其中\x5b是[的ascii代码。请注意,您需要转义\ of \x5b,因为否则"\x5b" == "[" ( C#会替换字符串)。通过转义它,是Regex“接收”\x5b并将其视为[。显然,可以使用@禁用转义序列展开。
@"\x5btwist]"https://stackoverflow.com/questions/18143123
复制相似问题