Search code examples
c#regexcapture-group

Help with a regex expression


I am trying to get the word after Sysdba. out of a string. Here is a example where my result would be PRODUCTION

CAST(CASE WHEN SYSDBA.PRODUCTION.SHIPPING_COMPLETE_DATE IS NOT NULL THEN 1 ELSE 0 END AS bit) AS [Shiping Completed]]

I created the regex expression

Regex CatagoryRegex = new Regex(@"SYSDBA\.(.#)\.", RegexOptions.IgnoreCase | RegexOptions.Compiled);

however when I call var tmp = CatagoryRegex.Match(SelectText); I get no matches. I know I am most likely making a simple mistake but I can not spot it.


Solution

  • Your specific problem is that .# will match "any character", followed by # character.

    The simplest fix might be to use .+ but this is a bit crude for what you appear to be doing. A step better is the non-greedy .+? which will only capture upto the next . (unless it has to go further), but I would probably go a step further than this even...

    To prevent . inside the captured group, try this expression:

    "SYSDBA\.([^.]+)\."
    

    Where [^.] is any character not a . (no need to escape in character classes), and the + means "one or more".

    If you want to potentially allow . inside your captured group, you could use:

    "SYSDBA\.(\S+)\."
    

    Where the \S is any non-whitespace character (so stops matching if spaces or newlines).