简体   繁体   English

正则表达式在 GREL/OpenRefine 中匹配

[英]regex with match in GREL/OpenRefine

I'm using OpenRefine to parse a column with string values.我正在使用 OpenRefine 解析带有字符串值的列。 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.contains()仅限于搜索一个词。

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 .我在文档中看到的是.match函数尝试将整个字符串 s 与正则表达式模式 p 进行匹配,并返回一组捕获组

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 (?!.*\\bdiscount\\b).*\\boffer\\b.*断言右边没有折扣并匹配任何字符和报价
    • | Or要么
    • (?!.*\\boffer).*\\bdiscount\\b.* Or assert the opposite (?!.*\\boffer).*\\bdiscount\\b.*或断言相反
  • ) Close non capturing group )关闭非捕获组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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