473,246 Members | 1,268 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

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 1667
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: alphatan | last post by:
Is there relative source or document for this purpose? I've searched the index of "Mastering Regular Expression", but cannot get the useful information for C. Thanks in advanced. -- Learning...
2
by: Christopher Ireland | last post by:
Hi -- Any ideas on this one please ... The following code works as expected: private void Form1_Load(object sender, System.EventArgs e) { string text = "The quick brown"+ " FOX " +"jumps over...
3
by: Steve | last post by:
The string in registry is "rundll32.exe C:\Program Files\INTERN~1\hmmapi.dll,OpenInboxHandler" After opensubkey and getregistry, I got "rundll32.exe \"C:\\Program...
2
by: Jeff Jarrell | last post by:
I want to use the regex.replace for a string containing "%s" I can't seem to get the "%s" escaped. I tried a normal "\%s" but that doesn't seem to do it. Picks up any "s"....
15
by: pkaeowic | last post by:
I am having a problem with the "escape" character \e. This code is in my Windows form KeyPress event. The compiler gives me "unrecognized escape sequence" even though this is documented in MSDN....
0
by: Asif Mohammed | last post by:
Hello, I have a datagridview bound to a database table with 2 columns. One is an ID column "NameID" which is hidden, the other is called "Name". The schema picture is here :...
0
by: Asif Mohammed | last post by:
Hello, I have a datagridview bound to a database table with 2 columns. One is an ID column "NameID" which is hidden, the other is called "Name". The schema picture is here :...
6
by: sloan | last post by:
I have a fairly simple RegEx code below. I am given a file name, (which I don't control) , and need to change a folder name in it. The code below is choking on the filename not being...
1
by: =?Utf-8?B?R3VoYW5hdGg=?= | last post by:
I have a string with some escape charaecters that need to be processed in our file Say for example the string might have something like following <escape V=".sp2" /> step 1: This has to be...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.