Linux: Using grep with a regular expression

Updated . Posted . Visible to the public.

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.

Last edit
Judith Roth
License
Source code in this card is licensed under the MIT License.
Posted to makandra dev (2016-12-06 21:10)