You are previewing grep Pocket Reference.

grep Pocket Reference

Cover of grep Pocket Reference by John Bambenek... Published by O'Reilly Media, Inc.
O'Reilly logo

Perl-Style Regular Expressions (grep -P)

Perl-style regular expressions use the Perl-Compatible Regular Expressions (PCRE) library to interpret the pattern and perform searches. As the name implies, this style uses Perl’s implementation of regular expressions. Perl has an advantage because the language was optimized for text searching and manipulation. As a result, PCRE can be more efficient and far more function-rich for finding content. The consequence is that it can be horribly messy and complex. To put it another way, using PCRE to find information is like using a weed whacker on yourself to do brain surgery: it gets the job done with minimum of effort, but it is an awful mess.

The specific search features and options with PCRE are not dependent upon grep itself, but use the libpcre library and the underlying version of Perl. This means that it can be highly variable between machines and operating systems. Usually the pcrepattern or pcre manpages will provide machine-specific information on the options that are available on your machine. What follows is a general set of PCRE search functions that should be available on most machines.

Also note that Perl-style regular expressions may or may not be present by default on your operating system. Fedora and Red Hat–based systems tend to include them (assuming you install the PCRE library), but Debian, for instance, does not enable Perl-style regular expressions by default in their grep package. Instead, they ship a pcregrep program, ...

The best content for your career. Discover unlimited learning on demand for around $1/day.