user1640934
user1640934

Reputation: 1

Why do I need to use '-P' when I grep a list?

I am using the -P option to grep a list of phone numbers. My results are excellent with this option, but the list is not returned without it. The grep man page says using -P will 'interpret PATTERN as a Perl regular expression'. Since I don't know Perl and am just learning grep and regex this isn't very meaningful to me.

Is there a simple explanation?

Upvotes: 0

Views: 90

Answers (2)

Jonathan Leffler
Jonathan Leffler

Reputation: 754020

There are 3 sets of regular expressions that could be used by grep:

  • BRE — basic regular expressions
  • ERE — extended regular expressions
  • PCREPerl-compatible regular expressions

Some of the concepts are the same between all these. For example, a character-class such as [a-z] or [^0-9] is the same in all of them. On the other hand, the meaning of ( vs \(, or whether | or + or | has a special significance, depends on the class of regular expressions.

This is a big topic; too big to cover easily in an SO answer. To find out more, look to Jeff Freidl's Mastering Regular Expressions book, and at the documentation for the difference sets of regular expressions.

Upvotes: 1

Birei
Birei

Reputation: 36262

Without the -P option grep interprets the pattern as a basic regular expression, and there are big differences between them, for example with metacharacters like ., ? or |. You will have to adapt your regexp.

Upvotes: 0

Related Questions