Regex : does not begin by “pattern”

grepregex

I have a lot of lines in my LIST file and want to list only lines whose name does not start by (or contains) "git".

So far I have :

cat LIST | grep ^[^g]

but I would like something like :

#not starting by "git"
cat LIST | grep ^[^(git)]
#not containing "git"
cat LIST | grep .*[^(git)].*

but it is not correct. What regex should I use ?

Best Answer

Using grep in this case with -P option, which Interprets the PATTERN as a Perl regular expression

grep -P '^(?:(?!git).)*$' LIST

Regular expression explanation:

^             the beginning of the string
 (?:          group, but do not capture (0 or more times)
   (?!        look ahead to see if there is not:
     git      'git'
   )          end of look-ahead
   .          any character except \n
 )*           end of grouping
$             before an optional \n, and the end of the string

Using the find command

find . \! -iname "git*"
Related Question