Reputation: 19
I have a list of ingredients and I need to highlight its allergen. However, there are some exceptions that need to be ignored. My regex is not ignoring composite strings (string with multiple words separated with space, e.g. Almond Milk).
In the example, I want to highlight MILK, EGGS, RICE and BUTTER. However, I want to ignore RICE MILK and GARLIC BUTTER, as shown below.
Beef Eggs(76%)(beef, cheese, rice milk, pork, milk), Seasoning (8%)(Thyme, basil, eggs, rosemary, tarragon, savoury butter, marjoram, oregano, and rice bay leaf), Provencal Sauce Water(16%)(Herbes de Provence, celery, chicken broth, butter, barley, lupin, garlic butter, and lemon juice)
My code on regex 101 => Example
(?<!rice milk|garlic butter)(?<=milk|eggs|butter|rice)|(?!rice milk|garlic butter)(?=milk|eggs|butter|rice)
Upvotes: 1
Views: 63
Reputation: 163507
You could match what you don't want, in this case rice milk
and garlic butter
and use a capture group what you do want using an alternation |
\b(?:rice milk|garlic butter)\b|\b(milk|eggs|rice|butter)\b
The values of group 1 are highlighted in green in the regex demo
Upvotes: 1
Reputation: 37460
I would suggest different approach, by splitting regex logic by each word:
(?:(?<!rice )milk|(?<!garlic )butter|eggs|rice(?! milk))
Explanation:
(?:..)
- non capturing group
(?<!rice )milk
- match milk
, but asser that what preceeds is not rice
(to exclude rice milk
) - other regex parts also follow same approach
|
- alternation operator
Upvotes: 1