bug-grep
[Top][All Lists]
Advanced

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

Re: Grep colors highlight all possible matches


From: Bob Proulx
Subject: Re: Grep colors highlight all possible matches
Date: Sat, 9 Feb 2013 17:52:34 -0700
User-agent: Mutt/1.5.21 (2010-09-15)

Pollock, Wayne wrote:
> In teaching REs, I use grep and recently noticed
> the colors highlight all possible matches, not
> the one match as per POSIX's rules:
> 
>   echo 'abcdefabc' | grep '[abc]'

The above won't generate any color.  Did you mean to use the color
option with that example?

  echo 'abcdefabc' | grep --color '[abc]'

> Should highlight only the first "a" (earliest match has
> precedence, than longest).  Instead, every "a", "b", and
> "c" is highlighted.

I disagree.  Because all of those a's and b's and c's do match as
well.  Consider this:

  $ echo 'abcdefabc' | sed 's/[abc]/x/g'
  xxxdefxxx

Or this:

  $ echo 'abcdefabc' | sed 's/[abc]/x/' | grep --color '[abc]'
  xbcdefabc

Even though I removed the first character that matched the later grep
still matches and still prints the line.  Therefore it isn't just the
first character but all of them that match.  And therefore grep is
doing the right thing.

> It would be my guess that grep historically didn't need to know
> exactly what matched, just that something did, so the code doesn't
> bother to apply any rules to determine exactly what matched.

Well...  The purpose of grep is to print lines that match the
pattern.  The ability to do other things is feature creep.

Of course you are looking for a tool that highlights and teaches
regular expressions.  That is great.  But should grep also be a
full featured RE teaching tool in addition to its primary purpose?
(And obviously with the way I asked that question I don't think it
should.  Sorry.)

I suggest that dedicated tool to teach regular expressions would be
relatively easy to write, I recall having seen several in my travels,
and would fit the needs much better than trying to coerce the feature
creep in grep.

Bob



reply via email to

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