Regular expression for word that doesn't match a list of words
Asked Answered
B

2

8

I need a regular expression to match words that are not in a specific list I have.

This is for a system I haven't made, but I need to use for filtering. Apparently it filters the fields according to the regular expression given, the fields contain only one word. So I want whole word matches.

For example I want any word other than tomato or potato, my regex so far:

^(?!(Potato|Tomato))

I am testing my regular expression here. When I input Potato I get:

Your pattern does not match the subject string.

That is the result I expect, but whenever I input anything else other than Tomato and Potato, like "chocolate" I get:

No match groups were extracted.

This means that your pattern matches but there were no (capturing (groups)) in it that matched anything in the subject string.

I tried changing my expression to:

([[:alnum:]])*^(?!(Potato|Tomato))

Meaning, I want any combination of alphanumeric characters except the words "Tomato" and "Potato", but I get the same results.

I don't know how to change my regex so it has a capturing group that matches what I need.

Blasphemous answered 6/7, 2015 at 19:55 Comment(1)
What language/tool do you use? You seem to write C# code, if it is for this language, it's better to use regexstorm.net/testerStumer
O
8

The regular expression that you are using

^(?!(Potato|Tomato))

This regular expression means "match the zero length string at the start which is not followed by 'Potato' or 'Tomato'"

The regex that you are using is a negative lookahead. Ref: here Moreover it does not capture anything because the only capturing group in this regex can only match "Potato" or "Tomato" which you have disallowed.

Capturing groups are defined by round braces but if there is a "?" in front of the left round brace, it is not treated as a capturing group.

If you want to have a capturing group in above regex use this :

^((?!(Potato|Tomato)))

Now if you test this regex against the string "bracket", you'll get a 0 length match at [0-0].

The regex that you are looking for is :

^(?!(Potato|Tomato)$)(\w*)
Overhand answered 6/7, 2015 at 20:33 Comment(0)
I
2

Your regex is a zero-length regex. Try this: ^(?!(Potato|Tomato))\w*. That will match chocolate. If you want to capture chocolate, then do this: ^(?!(Potato|Tomato))(\w*)

Demo

Illusive answered 6/7, 2015 at 20:2 Comment(1)
The above regex will not match 'Potatochocolate' which it should. DemoOverhand

© 2022 - 2024 — McMap. All rights reserved.