regexopenrefinegrel

regex with match in GREL/OpenRefine


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


Solution

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

    Regex demo

    That will match