【问题标题】:How to escape / char in grep -P '(?!.*//)word' to escape comments如何在 grep -P '(?!.*//)word' 中转义 / char 以转义评论
【发布时间】:2021-04-16 06:08:44
【问题描述】:

grep -P '(?!.*//)word' 我学会了在 grep 中使用 grep -P 和 perl 正则表达式来排除模式。但我不知道如何排除//

我试过了

  • /
  • \/
  • \\/

似乎没有任何效果

【问题讨论】:

  • / 并不特别......你能展示一些示例输入行和这些行所需的确切输出吗?如果你使用grep -P '^(?!.*//).*word',它可以工作吗?

标签: regex linux grep


【解决方案1】:

使用

grep -P '^\s*//.*(*SKIP)(*F)|word' file

regex proof

解释

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  //                       '//'
--------------------------------------------------------------------------------
  .*                       any character except \n (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
(*SKIP)(*F)               skip and proceed searching for the next pattern
--------------------------------------------------------------------------------
 |                        OR
--------------------------------------------------------------------------------
  word                     'word'

【讨论】:

    猜你喜欢
    • 2011-04-10
    • 1970-01-01
    • 2017-10-17
    • 1970-01-01
    • 2014-06-03
    • 2020-03-09
    • 1970-01-01
    • 2014-07-09
    • 2011-11-07
    相关资源
    最近更新 更多