Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 589
Dzyann Avatar asked Mar 15 '23 12:03

Dzyann


1 Answers

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*)
like image 148
Vaibhav Gupta Avatar answered Apr 02 '23 09:04

Vaibhav Gupta