C#regex无法通过管道分隔符拆分字符串

时间:2018-05-19 10:26:30

标签: c# regex split

string query = "A|B";

接下来的两行的输出等于"%A%|%B%",而预期“A%B”!

query = Regex.Replace(query, "|", "%");
query = Regex.Replace(query, @"|", "%");

为什么?

1 个答案:

答案 0 :(得分:4)

The | is a special character of regexes, it means "or"。你必须逃脱它。

query = Regex.Replace(query, @"\|", "%");