Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

According to grep --help and man grep, we can use the -P option in order to interpret the pattern as a Perl regular expression (PCRE, to be precise), instead of the default POSIX basic regular expressions (BRE).

In Perl language, various Modifiers can be added to the expression, in order to adjust the pattern interpretation (in the syntax of /pattern/modifiers).

So, how can someone add modifiers to the grep's Perl regular expression? I tried some variations like grep -P "/^got.it$/ms" [FILE] but the search results were wrong.

However, about the PCRE interpretation, the manual points out that:

This is highly experimental and grep -P may warn of unimplemented features.

Is it possible that the grep tool does not support modifiers at all?

By the way, I noticed that one can perform case-insensitive pattern matching by using the -i option, which is an example of a modifier.

share|improve this question
Crossposted: askubuntu.com/questions/410565/… ...which is okay, but you should indicate this! – goldilocks Jan 25 '14 at 14:35
up vote 8 down vote accepted

PCRE's are not literally perl; they use a stand-alone C library, libpcre. Sometimes "PCRE style" is also used to refer to, e.g., other languages which implement regexps using the same/similar patterns, even though they do not rely on libpcre (note grep does use the actual libpcre).

Strictly speaking, a regular expression doesn't really include modifiers,1 it's just a pattern. Some regexp interfaces (such as perl's) allow for the use of modifiers along with the patterns; grep doesn't.

There are a few differences in terms of the pattern semantics too. Again, keep in mind it's not actually perl, it's something based on it.

1. Unless they're actually inside the pattern, which you can do with grep. E.g. grep -P "(?i)foobar" will be a case insensitive match.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.