Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Regular Expression matching but not Regexr.com

Tags:

c#

regex

Maybe I am doing something wrong here but it seems weird to me that when I try to match the string "radiologic examination eye detect foreign body" against the regular expression "\b*ct\b" on regexr.com then I find no match but when I try and do the same thing using a C# program, it matches. C# code is given below. Am I doing/checking something wrong?

string desc = "radiologic examination eye detect foreign body";
string regex = "\\b" + "*ct" + "\\b";
if (Regex.IsMatch(desc, regex))
{
    String x = Regex.Replace(desc, regex, " " + "ct" + " ").Trim();
    Console.WriteLine(x);
}

Thanks in advance!

like image 742
Kumar Vaibhav Avatar asked Oct 01 '22 20:10

Kumar Vaibhav


1 Answers

It's matching because you've got an asterisk in there.

An asterisk means:

Matches the preceding character zero or more times

So it's not matching the \b at all, but still satisfying the above condition.

Remove this and it no longer matches:

string regex = "\\b" + "ct" + "\\b";

As for why it doesn't match on Regexr, I don't know, but for me it does actually match on reFiddle

like image 109
mattytommo Avatar answered Oct 05 '22 10:10

mattytommo