468,719 Members | 1,854 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,719 developers. It's quick & easy.

Escape regex to normal string

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.
Nov 16 '05 #1
2 1555
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;
}

FrzzMan wrote:
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.

Nov 16 '05 #2

Check out the shared method Regex.Escape

Brian Davis
http://www.knowdotnet.com
"FrzzMan" <Fr***************@vnOCzone.com> wrote in message
news:OA*************@TK2MSFTNGP09.phx.gbl...
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.

Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Christopher Ireland | last post: by
3 posts views Thread by Steve | last post: by
2 posts views Thread by Jeff Jarrell | last post: by
15 posts views Thread by pkaeowic | last post: by
1 post views Thread by =?Utf-8?B?R3VoYW5hdGg=?= | last post: by
9 posts views Thread by bryonone | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.