Linux: Using grep with a regular expression

You can use three different versions of the regular expression syntax in grep:

  • basic: -G
  • extended: -E(POSIX)
  • perl: -P (PCRE)

Difference between basic and extended Show archive.org snapshot :

In basic regular expressions the meta-characters '?', '+', '{', '|', '(', and ')' loose their special meaning; instead use the backslashed versions '?', '+', '{', '|', '(', and ')'.

Difference between extended (POSIX) and perl (PCRE): E.g. \d is not supported in POSIX.

This grep command searches for the same pattern in different regular expression versions.

grep -G "[0-9]\{4\}ms" production.log
grep -E "[0-9]{4}ms" production.log
grep -P "\d{4}ms" production.log

As a Ruby dev, you may want to choose the Perl syntax, because it's more similar to how Ruby's regular expressions work than the others.

Andreas Robecke Over 7 years ago