473,386 Members | 1,715 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,386 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 1672
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.