1

1) using the c# regex

i currently have the following regex

^(abc|def)$

it returns true if the word is "abc" or "def" what i need is for it to match anything but those two words, including strings that contain those words. i currently do it like this

Regex rgx = new Regex("^(abc|def)$");

if(!rgx.IsMatch(somestring)){
// do stuff
}

what i want is a regex where i don't have to use the ! operator. so i need something like (see the ! operator in new regex, but it doesn't work.)

Regex rgx = new Regex("^(!(abc|def))$");

if(rgx.IsMatch(somestring)){
// do stuff
}

expected results for somestring

blah --> true

abc blah --> true

abc --> false

def --> false

blah def --> true

Hope this makes sense.. thanks in advance.

and just to clarify, i'm not trying to find the word in a string, i want to compare the whole string to the regex... hence the ^()$

as far as why not just use !rgx.IsMatch, let's just say i'm simply trying to see if it's possible with regex

robert
  • 1,455
  • 4
  • 19
  • 27
  • not a duplicate, read the question... i'm looking for a whole word match not a match contained inside a string – robert Apr 17 '14 at 03:36
  • If you are not trying to find word in a string using regex why use regex? Does string.Contains and simple condition won't work? – jomsk1e Apr 17 '14 at 03:37
  • the "why" isn't important here. i wanted to see if it's possible. – robert Apr 17 '14 at 03:43

1 Answers1

4

You can use this regex. It checks that the start mark ^ is not followed by abc or def until the end $.

^(?!(abc|def)$).*
Szymon
  • 41,313
  • 16
  • 90
  • 109