Problem with RegEx
I have an issue with RegEx. Here is an example of what I am doing and what I am expecting and what I get.
Text:
Glenn Inman
Expression:
(?<!(?i:\bhoward\b))\s+(?i:\bglenn\b).*
Code:
bool bMatch = RegEx.IsMatch("Glenn Inman", "(?<!(?i:\bhoward\b))\s+(?i:\bglenn\b).*")
Returns false
expect true
Have tested in RegExBuddy, EditPad Pro and it works
Please tell me what I have done incorrectly
[536 byte] By [
hglenni] at [2008-2-15]
hglenni,
It seems perfectly reasonable that it returns false:
Your first condition (?<!(?i:\bhoward\b)) is satisfied, but the following \s+ is not as there is no space before "Glenn", hence the string does not match. If you try " Glenn Inman" instead (there is a single space before Glenn), bMatch will be true.
I think you can safely change \s+ to \s* as your condition \bglenn\b already makes sure you only get occurrences for glenn on word boundaries. See if this would work for you:
bool
bMatch = Regex.IsMatch("Glenn Inman", @"(?<!(?i:\bhoward\b))\s*(?i:\bglenn\b).*");HTH
--mc
Thank you
Actually what turns out to be what I was looking for is
(?<!(?i:\bhoward\b\s+))(?i:\bglenn\b).*
That does get me what I wanted - but your notice of the nonspace was real helpful
thanks