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?

Rezaeimh7
  • 1,467
  • 2
  • 23
  • 40

1 Answers1

5

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

query = Regex.Replace(query, @"\|", "%");
xanatos
  • 109,618
  • 12
  • 197
  • 280