4

I am getting the hang of grep and regex, but I am confused about the different options.

grep --help | grep -i "reg.*ex"
  -E, --extended-regexp     PATTERN is an extended regular expression (ERE)
  -G, --basic-regexp        PATTERN is a basic regular expression (BRE)
  -P, --perl-regexp         PATTERN is a Perl regular expression

I have a general knowledge of regular expressions, but often I find myself stumbling on the basics (e.g. Using '*' when it actually requires '.*'), which I blame on not knowing the differences between the types of regular expressions (ERE, BRE, Perl regex).

Is it explained somewhere in detail?

Stephen Kitt
  • 434,908
  • I have voted to close this as off-topic ("request for tutorial/learning materials"). It would be a too big topic to correctly write in an answer on this site. The Wikipedia article on regular expressions hold much information, as should the perlre and re_format manuals on your system. See also the "see also" section of your grep manual. Do come back with specific question about any of these type of expressions though. – Kusalananda Feb 28 '18 at 08:06
  • see also: https://unix.stackexchange.com/questions/119905/why-does-my-regular-expression-work-in-x-but-not-in-y – Sundeep Feb 28 '18 at 08:15
  • 1
    @Sundeep: perl's regex are not explained (only pcre) in your link. Perl is the most advanced regex engine ever. – Gilles Quénot Feb 28 '18 at 08:23
  • @GillesQuenot can you clarify? I see links for both pcre and perldoc... and grep uses PCRE (as per GNU grep manual) – Sundeep Feb 28 '18 at 08:29
  • PCRE is not perl, just a subset of perl's regex – Gilles Quénot Feb 28 '18 at 08:30
  • Using '*' when it actually requires '.*' sounds lie you're confusing regular expressions and shell globs - rather than different RE flavors. – steeldriver Feb 28 '18 at 10:30

1 Answers1

3

Answering the specific question about *.

The * special character in a regular expression (of any type) acts on the previous expression. It allows for zero or more matches of the previous expression. The regular expression .* therefore matches any string, whether it's empty or not.

The * filename globbing character matches any (possibly empty) string.

Regular expressions are not filename globbing patterns (or vice versa). This particular difference is between regular expressions and globbing patterns, and not a difference between different types of regular expressions.

Kusalananda
  • 333,661