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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With