1
string query = "A|B";

The output of the next 2 lines are equal to "%A%|%B%" whereas "A%B" is expected!

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

Why?

m.r226
  • 1,348
  • 2
  • 18
  • 35

1 Answers1

4

The | is a special character of regexes, it means "or". You have to escape it.

query = Regex.Replace(query, @"\|", "%");
xanatos
  • 106,283
  • 12
  • 188
  • 265