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

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

Re: "grep -o" skips some matching patterns


From: Michael Elizabeth Chastain
Subject: Re: "grep -o" skips some matching patterns
Date: Tue, 30 Dec 2003 14:52:32 -0500 (EST)

> $ echo abc0111def | grep "[01]*"
> abc0111def
> $ echo abc0111def | grep -o "[01]*"
> 
> "grep -o" does not print the matching string, although the return value
> indicates a successful match.

You are getting bitten by an empty string match.

The pattern "[01]*" matches an empty string.  The match is succeeding
at the beginning of "abc0111def", before the 'a' character!

  "" "abc011def"
  ^^ matching text

That is the leftmost match, so it takes priority over this match:

  "abc" "0111" "def"
        ^^^^^^ matching test

Then "grep -o" happily prints the empty string and returns success.

> However, removing the first characters from
> the input does work:
> 
> $ echo 0111def | grep -o "[01]*"
> 0111

Now there are several matches at position 0 of the string:

  "" "0111def"
  "0" "111def"
  "01" "11def"
  "011" "1def"
  "0111" "def"

These matches all start at the same position, so grep chooses
the longest match.

The rules are:

  (1) Left-most position.
  (2) At the same position, then longest match.

A pattern which matches the empty string always succeeds at the
beginning of the target, even when it matches an empty string at the
beginning rather than the stuff you really want in the middle.
Yes, this can be annoying.

Maybe you want "[01]+" ?

Michael C
GDB QA Guy




reply via email to

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