Escape regex to normal string

  • Thread starter Thread starter FrzzMan
  • Start date Start date
F

FrzzMan

Hello,

Is there any function that automatically escape all regex operator in a
string? So that the string that contain regex operator will be
identified as a *string*, not a *regex pattern*.

If there's no function like that, if you have time and it is not so
complex, can you help me code one?

Thanks.
 
I code this function, is it enough?

public static string EscapeAll(string p_Pattern)
{
string fm_String = p_Pattern;

// Escape these characters
// . $ ^ { [ ( | ) * + ? \

// Always escape '\' first
fm_String = fm_String.Replace("\\", "\\\\");
fm_String = fm_String.Replace(".", "\\.");
fm_String = fm_String.Replace("$", "\\$");
fm_String = fm_String.Replace("^", "\\^");
fm_String = fm_String.Replace("{", "\\{");
fm_String = fm_String.Replace("[", "\\[");
fm_String = fm_String.Replace("(", "\\(");
fm_String = fm_String.Replace("|", "\\|");
fm_String = fm_String.Replace(")", "\\)");
fm_String = fm_String.Replace("*", "\\*");
fm_String = fm_String.Replace("+", "\\+");
fm_String = fm_String.Replace("?", "\\?");

return fm_String;
}
 
Back
Top