Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Word boundary issue in a dynamic C# regex pattern

Tags:

c#

regex

I am trying to build a regex pattern with some parts of pattern retrieved from a database.

For example

string pt= "@\"\\b(" + db.GetPattern + ")\\b\"";        
Regex regex = new Regex(pt, RegexOptions.IgnoreCase | RegexOptions.Compiled);
return regex.Replace(input, "*");

Although I tried to escape it, I couldn't get it to work. If I build the pattern manually as

Regex regex = new Regex(@"\b(test|test2)\b", RegexOptions.IgnoreCase | RegexOptions.Compiled);
return regex.Replace(input, "*");

it works fine.

like image 817
nLL Avatar asked Sep 24 '09 12:09

nLL


1 Answers

Try

string pt = @"\b(" + db.GetPattern + @")\b";

or alternatively:

string pt = string.Concat(@"\b(", db.GetPattern, @")\b");

The basic reason is that the pattern you give as an example and the string you are building are quite different. Things like literal strings, such as @"foo" only matter to the C# compiler, after compilation all strings are equal. The two strings @"\b" and "\\b" are completely equal, the only difference is in how the C# compiler evaluates escape characters inside it. Which means that there is absolutely no need to incorporate C# syntax into strings you use at runtime.

Basically the string you created contained:

@"\b(test|test2)\b"

whereas you wanted the pattern

\b(test|test2)\b

So parts like the @ sign and the quotation marks were literals for the regex. Thus it wouldn't match.

like image 88
Joey Avatar answered Nov 10 '22 03:11

Joey