1

This question already has an answer here:

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?

marked as duplicate by Kusalananda, user34720, Jeff Schaller, Stephen Kitt, Romeo Ninov Feb 28 '18 at 16:41

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 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 8:06
  • see also: unix.stackexchange.com/questions/119905/… – Sundeep Feb 28 '18 at 8:15
  • 1
    @Sundeep: perl's regex are not explained (only pcre) in your link. Perl is the most advanced regex engine ever. – Gilles Quenot Feb 28 '18 at 8: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 8:29
  • PCRE is not perl, just a subset of perl's regex – Gilles Quenot Feb 28 '18 at 8:30
2

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.

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