简体   繁体   中英

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

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

  • (?: 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM