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.


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*)

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

链接地址: http://www.djcxy.com/p/2162.html

上一篇: 正则表达式匹配不包含单词的行

下一篇: 正则表达式与单词列表不匹配