Search code examples
c#regexsearchtext-search

C# Regular expression search gives false


I am trying to do a program that searches for certain tags inside textfiles and see if there is text in between those tags. Example of tags below.

--<UsrDef_Mod_Trigger_repl_BeginMod>
--<UsrDef_Mod_Trigger_repl_EndMod>

So i want to search for --<UsrDef_Mod_ and _Begin or _End

I made these RegExp, but i get false on every single one.

if (Regex.Match(line, @"/--<UsrDef_Mod_.*_BeginMod>/g", RegexOptions.None).Success)
else if (Regex.Match(line, @"/--<UsrDef_Mod_.*_EndMod>/g", RegexOptions.None).Success)

So any help to find where im going wrong. I have used regexr.com to check my regexp and its getting a match there but not in C#.


Solution

  • The .NET library Regex doesn't understand the "/ /g"wrapper.

    Just remove it:

    // Regex.Match(line, @"/--<UsrDef_Mod_.*_BeginMod>/g", 
       Regex.Match(line, @"--<UsrDef_Mod_.*_BeginMod>",