GNU bug report logs - #33062
grep matches too lazily when invoked with -zoP and matching backreference followed by newline

Previous Next

Package: grep;

Reported by: Johannes Riecken <johannes.riecken <at> gmail.com>

Date: Tue, 16 Oct 2018 14:42:02 UTC

Severity: normal

Tags: notabug

Done: Paul Eggert <eggert <at> cs.ucla.edu>

Bug is archived. No further changes may be made.

Full log


View this message in rfc822 format

From: Jim Meyering <jim <at> meyering.net>
To: johannes.riecken <at> gmail.com
Cc: 33062 <at> debbugs.gnu.org
Subject: bug#33062: grep matches too lazily when invoked with -zoP and matching backreference followed by newline
Date: Sun, 28 Oct 2018 12:19:09 +0000
tags 33062 notabug
close 33062
stop

On Tue, Oct 16, 2018 at 2:42 PM Johannes Riecken
<johannes.riecken <at> gmail.com> wrote:
> *Bug summary: *GNU grep 3.0-2 matches too lazily when invoked with -zoP and
> matching backreference followed by newline
>
> *Steps to reproduce:*
>
> Create file cases:
>
> foo
> bar
>   cases:
>     1: foo
>     2: bar
> baz
>   cases:
>     3: baz
> quux
>
> And run grep -zoP '(\s*)cases:\n(\1.*\n)*' cases
>
> *Expected output:*
>
>   cases:
>     1: foo
>     2: bar
>   cases:
>     3: baz
>
> * Actual output:*
>
>   cases:
>
>   cases:

Thanks for the report.
However, this is not a bug.
The \s in your regexp matches the preceding newline, too, and that
forces the behavior you see.
I presume you wanted something like this:

$ printf '%s\n' foo bar '  cases:' '    1: foo' '    2: bar' baz '
cases:' '    3: baz' quux\
  | grep -zoP '( *)cases:\n(\1.*\n)*'
  cases:
    1: foo
    2: bar
  cases:
    3: baz

The adjusted regexp works because it accepts only spaces in the group.
Don't be tempted to allow TABs, but rather ensure the input is
expanded to use only spaces for indentation.




This bug report was last modified 5 years and 201 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.