bug-gnu-utils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [grep 2.5.1] odd output for some patterns combining backreference, a


From: Bauke Jan Douma
Subject: Re: [grep 2.5.1] odd output for some patterns combining backreference, alternation, and repetition
Date: Tue, 02 Oct 2007 00:10:20 +0200
User-agent: Thunderbird 2.0.0.6 (X11/20070728)

Alex Lamey wrote on 30-09-07 22:03:
Hi,
I think grep (2.5.1) is misbehaving on me. Here are twelve examples, wherein I apply six patterns (differing only in the repetition argument) to two inputs (differing only in their fifth character). Five of the outputs (marked '!') seem to be wrong to me. But I'm quite new to grep so maybe I've misunderstood something.

$ echo 88-88 | egrep -o '([0-9])([0-9])-(\1|\2){1}'
88-8

$ echo 88-88 | egrep -o '([0-9])([0-9])-(\1|\2){2}'
88-88

$ echo 88-88 | egrep -o '([0-9])([0-9])-(\1|\2){3}'
88-88
    !

$ echo 88-88 | egrep -o '([0-9])([0-9])-(\1|\2){4}'
88-8
    !!

$ echo 88-88 | egrep -o '([0-9])([0-9])-(\1|\2){5}'
88-88
    !

$ echo 88-88 | egrep -o '([0-9])([0-9])-(\1|\2){6}'
88-88
    !

$ echo 88-81 | egrep -o '([0-9])([0-9])-(\1|\2){1}'
88-8

$ echo 88-81 | egrep -o '([0-9])([0-9])-(\1|\2){2}'
88-8
    !

$ echo 88-81 | egrep -o '([0-9])([0-9])-(\1|\2){3}'

$ echo 88-81 | egrep -o '([0-9])([0-9])-(\1|\2){4}'

$ echo 88-81 | egrep -o '([0-9])([0-9])-(\1|\2){5}'

$ echo 88-81 | egrep -o '([0-9])([0-9])-(\1|\2){6}'

If this isn't a bug, but a mistake on my part, an explanation would be appreciated.
Thanks,
-Alex




Not a bug -- assuming the ! is and envisualization of your surprise.

The [0-9] means just one digit in that range.  So each of these
two constructs matches at most just one digit.  Hence \1 can only be
a reference to at most one digit.  Same for \2.  And hence the same
for \1|\2.
Therefore {1} gives you just one digit on the right side of the dash,
{2} gives you 2, and anything else just doesn't match, because you're
asking too many: {n} means /exctly/ n matches.

bjd







reply via email to

[Prev in Thread] Current Thread [Next in Thread]