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
