I'm using OpenRefine to parse a column with string values. I want to find the cells that contain either: offer or discount. The string value is usually a sentence
My code below is using the match function not working.
using value.contains()
is limited to searching for one word only.
value.match(/.*(offer)|(discount)/)
What I can see in the documentation is that the .match
function Attempts to match the string s in its entirety against the regex pattern p and returns an array of capture groups.
To match either one of them but not both, you might use a positive and a negative lookahead if that is supported.
To match either of the options, use an alternation to make sure one of the words is there and the other one is not and vice versa:
(?:(?!.*\bdiscount\b).*\boffer\b.*|(?!.*\boffer).*\bdiscount\b.*)
That will match
(?:
Non capturing group
(?!.*\bdiscount\b).*\boffer\b.*
Assert that on the right is no discount and match any char and offer|
Or(?!.*\boffer).*\bdiscount\b.*
Or assert the opposite)
Close non capturing group