views:

41

answers:

2

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.

A: 

You probably mean .* instead of .#.

Sjoerd
.* would be too greedy and gobble up everything... well, everything up to the next escaped period.
Pretzel
+2  A: 

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).

Peter Boughton
I withdrew my answer. I think this is the best answer so far. +1
Pretzel
I thouht # was the non greedy version of * insted of +? that was my stupid mistake.
Scott Chamberlain
Thanks Pretzel. I almost did go the `\w` route, but I don't know enough about what might be accepted/required to match (e.g. if this is TSQL then `[` and `]` might be involved). Anyway, I've updated a bit further - it's still not as complete/thorough as it could be, but I have to go now - if anyone thinks of any more to add feel free to leave a comment.
Peter Boughton
Will \S match underscores?
Scott Chamberlain
Scott, I've never seen that, though I don't use .NET regex so it's possible. However, the only 'special use' for `#` I'm aware of with any regex is for comments - e.g. `(?x)#endofline` or `(?#inline)`.
Peter Boughton
Yes, `\S` will match the exact opposite of `\s`, and `\s` is effectively `[ \t\n\r]` (and maybe other whitespace chars). If you want to exclude underscores, you can use either `(?!_)\S` or `[^\s_]` to do that.
Peter Boughton