0

Can someone explain what the following expressions mean with grep? How is that different from egrep?

With grep:

  1. ^[0-9]\+$
  2. [0-9]\{3\}.*[0-9]\{3\}
  3. ^.*[0-9]$

With egrep:

  1. ^[0-9]{7}$
  2. ^.*$

1 Answer 1

2

egrep started up as a new grep implementation in Unix V7 in the late 70s with a new regexp engine and syntax (awk, also released V7 used that same new extended regexp (ERE) syntax instead of the one used by grep/ed/sed). Since then, POSIX has merged the feature of the egrep command into grep with the -E option and deprecated egrep.

Nowadays, on some systems, egrep is just a sh-script which just does something like exec grep -E "$@" and is just there for backward compatibility, to accommodate scripts that haven't been updated to use grep -E instead.

$ cat /usr/bin/egrep
#!/bin/sh
exec grep -E "$@"

so, there egrep is just grep -E with no any differences at all.


The ^ is used for start-of-line anchor and accordingly $ is used for end-of-line anchor.
This [0-9] matches collating elements in the 0 to 9 range in your locale. That used to only include 0123456789, but depending on the system and locale may include many more.
This {min,Max} is known interval expression.
This .* matches any sequence of 0 or more characters.

EREs (as in egrep / awk) introduced a few new operators: +, ? and | and removed back-reference support.

The grouping operators which where \(...\) in BREs (basic regexps as used in grep) are (...) instead in EREs.

Regexps didn't initially have interval operators. They were first added as \{x,y\} in BREs, but not in EREs as doing so would have broken backward compatibility. That was changed by POSIX which did specify {x,y} for EREs in the 90s. To this day, there are still egrep or awk implementations that still don't support it though.

The GNU implementation of grep (and other utilities using BREs) has gone further and added the +, ?, | ERE operators to BREs as \+, \?, \| and does support back-references with EREs (grep -E) as non-standard extensions for consistency, so in those implementations, BREs and EREs are functionally equivalent, only the syntax is different.

see also Why does my regular expression work in X but not in Y?.

And here in https://www.regular-expressions.info/posix.html#bre also I found very good information about ERE/BRE and some more;

I found this regex comparison also from here https://www.regular-expressions.info/refrepeat.html very useful and combined 4 of them as following:

Feature Syntax Description                                                                   Example GNU BRE GNU ERE POSIX BRE POSIX ERE
Greedy quantifier ? (question mark) Makes the preceding item optional. Greedy, so the optional item is included in the match if possible. abc? matches abc or ab no YES no YES
Greedy quantifier ? Makes the preceding item optional. Greedy, so the optional item is included in the match if possible. abc? matches abc or ab YES no no no
Greedy quantifier * (star) Repeats the previous item zero or more times. Greedy, so as many items as possible will be matched before trying permutations with less matches of the preceding item, up to the point where the preceding item is not matched at all. ".*" matches "def" "ghi" in abc "def" "ghi" jkl YES YES YES YES
Greedy quantifier + (plus) Repeats the previous item once or more. Greedy, so as many items as possible will be matched before trying permutations with less matches of the preceding item, up to the point where the preceding item is matched only once. ".+" matches "def" "ghi" in abc "def" "ghi" jkl no YES no YES
Greedy quantifier + Repeats the previous item once or more. Greedy, so as many items as possible will be matched before trying permutations with less matches of the preceding item, up to the point where the preceding item is matched only once. ".+" matches "def" "ghi" in abc "def" "ghi" jkl YES no no no
Fixed quantifier {n} where n is an integer >= 1 Repeats the previous item exactly n times. a{3} matches aaa no YES no YES
Greedy quantifier {n,m} where n >= 0 and m >= n Repeats the previous item between n and m times. Greedy, so repeating m times is tried before reducing the repetition to n times. a{2,4} matches aaaa, aaa or aa no YES no YES
Greedy quantifier {n,} where n >= 0 Repeats the previous item at least n times. Greedy, so as many items as possible will be matched before trying permutations with fewer matches of the preceding item, up to the point where the preceding item is matched only n times. a{2,} matches aaaaa in aaaaa no YES no YES
Greedy quantifier {,m} where m >= 1 Repeats the previous item between zero and m times. Greedy, so repeating m times is tried before reducing the repetition to zero times. a{,4} matches aaaa, aaa, aa, a, or the empty string no YES no no
Fixed quantifier {n} where n is an integer >= 1 Repeats the previous item exactly n times. a{3} matches aaa YES no YES no
Greedy quantifier {n,m} where n >= 0 and m >= n Repeats the previous item between n and m times. Greedy, so repeating m times is tried before reducing the repetition to n times. a{2,4} matches aaaa, aaa or aa YES no YES no
Greedy quantifier {n,} where n >= 0 Repeats the previous item at least n times. Greedy, so as many items as possible will be matched before trying permutations with fewer matches of the preceding item, up to the point where the preceding item is matched only n times. a{2,} matches aaaaa in aaaaa YES no YES no
Greedy quantifier {,m} where m >= 1 Repeats the previous item between zero and m times. Greedy, so repeating m times is tried before reducing the repetition to zero times. a{,4} matches aaaa, aaa, aa, a, or the empty string YES no no no
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .