Question

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

Était-ce utile?

La solution

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

^(?!(abc|def)$).*
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top