【问题标题】:Grep regex to find duplicate words while excluding a list of keywordsGrep 正则表达式查找重复的单词,同时排除关键字列表
【发布时间】:2015-05-13 11:12:09
【问题描述】:

我在一个目录中有很多数据,我想找到任何不是数字的双字实例。我从here开始:

\b(\w+) \1\b

并将其扩展为在结果中包含我不想要的内容:

(?!(?:one|two|three|four|five|six|seven|eight|nine|oh|zero))\b(\w+) \1\b

当我将它作为 python 表达式放入 regex101 时,这有效(因为这是我所熟悉的),但当我在 grep 命令中使用它时无效。我意识到我不能使用 !,所以我在阅读 this question 后尝试了这个:

 grep -Proh "\b(\w+) \1\b" | grep -Prohv "?(?:one|two|three|four|five|six|seven|eight|nine|oh|zero)"

返回“grep:没什么可重复的”。我不确定我是否使用了正确的 grep 参数,或者我正在使用的正则表达式有什么问题。

要匹配的样本数据:
今天来评估可能性。怀疑这种情况正在发生

要忽略的示例数据:
比重一点零零七

【问题讨论】:

  • 与 -P \b(?!(?:eight|f(?:ive|our)|nine|o(?:h|ne)|s(?:even|ix)|t(?:hree|wo)|zero))(\w+) \1\b一起使用

标签: regex bash grep


【解决方案1】:

只需-P-oP 就足够了。

$ grep -P '(?!(?:one|two|three|four|five|six|seven|eight|nine|oh|zero))\b(\w+) \1\b' file
today to evaluate for possibilities. doubt that that is occurring
$ grep -oP '(?!(?:one|two|three|four|five|six|seven|eight|nine|oh|zero))\b(\w+) \1\b' file
that that

【讨论】:

  • 行得通!我确实需要其他参数来保持数据的可管理性,但添加它们不会破坏任何东西。最终破坏事物的结果是单引号和双引号,将我的与您的进行比较。有什么区别?
  • 我认为问题主要是因为额外的hv参数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-02
  • 2016-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-12
  • 2011-01-05
相关资源
最近更新 更多