How to escape / char in grep -P '(?!.*//)word' to escape comments

Viewed 73

grep -P '(?!.*//)word' i learnt to exclude a pattern in grep using grep -P with perl regex. but i don't know how to exclude //

i tried

  • /
  • \/
  • \\/

nothing seems to work

1 Answers

Use

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

See regex proof

EXPLANATION

--------------------------------------------------------------------------------
  ^                        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'
Related