POSIX specifies that ERE chooses the leftmost longest match (see
re_format(7) on BSD):
In the event that an RE could match more than one substring of a given string, the RE matches the one starting earliest in the string. If the RE could match more than one substring starting at that point, it matches the longest.
“Earliest” (i.e. “leftmost”) is consistent with PCRE, but “longest” is not. To
illustrate the difference, consider the pattern
/a?(ab)?/ and the string
"ab". Note that the possible matches are
"a", and then
/(ab)?/ matches the empty
python all exhibit this behavior:
ERE, on the other hand, matches
"ab", the longest of the possible matches.
sed -r, and
awk serve as examples of this:
"a" (using the pattern