SwissCoder
SwissCoder

Reputation: 2570

convert any string to be used as match expression in regex

If you have a string with special characters that you want to match with:

System.Text.RegularExpressions.Regex.Matches(theTextToCheck, myString);

It will obviously give you wrong results, if you have special characters inside myString like "%" or "\".

The idea is to convert myString and replacing all occurences of special characters like "%" to be replaced by their corresponding characters.

Does anyone know how to solve that or does someone have a RegEx for that? :)

Update: The following characters have a special meaning, that I should turn of with adding a leading backslash: \, &, ~, ^, %, [, ], {, }, ?, +, *,(,),|,$

are there any others I should replace?

Upvotes: 1

Views: 238

Answers (3)

JotaBe
JotaBe

Reputation: 39004

Regex.Escape will do that for you. Somewhere in msdn doc it reads:

Escape converts a string so that the regular expression engine will interpret any metacharacters that it may contain as character literals

which is much more informative that the function description.

This is left for search/replace reference.

Use this as your regex:

(\\|\&|\~|\^|\%|\[|\]|\{|\}|\?|\+|\*|\(|\)|\||\$)

gets your chars of interes in a numbered group

And this as your replacement string:

\$1

replaces the matches with backslash plus the group content

Sample code:

Regex re = new Regex(@"(\\|\&|\~|\^|\%|\[|\]|\{|\}|\?|\+|\*|\(|\)|\||\$)");
string replaced = re.Replace(@"Look for (special {characters} and scape [100%] of them)", @"\$1");

Upvotes: 0

Jens
Jens

Reputation: 25563

If you want to escape all characters that carry a special meaning in regex, you could simply escape every character with a backslash (There is no harm in escaping characters that don't need to be escaped).

But if you do, why are you using Regex at all instead of string.IndexOf?

Upvotes: 1

Oded
Oded

Reputation: 498942

As @Kobi links to in the comments, you need to use Regex.Escape to ensure that that regular expression string is properly escaped.

Escapes a minimal set of characters (\, *, +, ?, |, {, [, (,), ^, $,., #, and white space) by replacing them with their escape codes. This instructs the regular expression engine to interpret these characters literally rather than as metacharacters.

Upvotes: 2

Related Questions