I've reproduced the problem, and it appears to be a change in the
newlib regular expression implementation, for a case that POSIX
leaves undefined.

I have bash 5.3 built from source, which apparently is a bit more
verbose than the default /usr/bin/bash.exe.

Here's a simpler example:

$ [[ x =~ ^(foo|)$ ]] ; echo $?
bash: [[: invalid regular expression `^(foo|)$': empty (sub)expression
2
$ [[ '' =~ ^(foo|)$ ]] ; echo $?
bash: [[: invalid regular expression `^(foo|)$': empty (sub)expression
2
$·

The expected result is 1 (no match) for the first and 0 (match) for
the second.  A result of 2 indicates an invalid regular expression.

The error message is from newlib/libc/posix/regerror.c or
winsup/cygwin/regex/regerror.c (I presume the latter is used
in Cygwin).

The behavior is documented in the regex(3) man page:

     ‘|’ cannot appear first or last in a (sub)expression or
     after another ‘|’, i.e., an operand of ‘|’ cannot be
     an empty subexpression.  An empty parenthesized subexpression,
     ‘()’, is legal and matches an empty (sub)string.  An empty
     string is not a legal RE.

So a workaround is to change the regular expression from
'^(foo|)$' to '^(foo|())$'.

"grep -E" accepts the empty subexpression without complaint.

On Ubuntu, both bash and "grep -E" accept the regular expression
with an empty subexpression is accepted without complaint.

POSIX leaves this explicitly undefined.

https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap09.html#tag_09_04_03

    The <vertical-line> is special except when used in a bracket
    expression (see 9.3.5 RE Bracket Expression). A <vertical-line>
    appearing first or last in an ERE, or immediately following
    a <vertical-line> or a <left-parenthesis>, or immediately
    preceding a <right-parenthesis>, produces undefined results.

-- 
Problem reports:      https://cygwin.com/problems.html
FAQ:                  https://cygwin.com/faq/
Documentation:        https://cygwin.com/docs.html
Unsubscribe info:     https://cygwin.com/ml/#unsubscribe-simple

Reply via email to