[sf-lug] On learning regular expressions

Asheesh Laroia asheesh at asheesh.org
Mon Dec 29 14:45:58 PST 2008


On Mon, 29 Dec 2008, Michael Paoli wrote:

>> Date: Sun, 14 Dec 2008 18:12:12 -0800 (PST)
>> From: Asheesh Laroia <asheesh at asheesh.org>
>>
>> On Sun, 14 Dec 2008, Jesse Zbikowski wrote:
>>
>>> Forget about the regular expression capabilities in the standard Unix
>>> commands unless you are a masochist.
>>
>> Or (my preference) get used to demand "E"xtended regular expressions.
>> $ grep -E
>> $ sed -r
>>
>> Those two give you a perl-ish modern set of regular expressions where you
>> don't find yourself cursing the tool for being stuck in 1988.  It's
>> probably not "full" perl-compatible regular expressions, but I've always
>> been happy with how close it is.
>>
>> I firmly believe we need to stop teaching "grep and sed" and start
>> teaching people to use "grep -E and sed -r".
>
> Actually, egrep and grep -E are "just" extended regular expressions.

Yes, but with that comes syntax and escaping changes; switching from one 
to another drives me batty.  For example:

The way I expect things (sed with -r):

 	paulproteus at vellum:~ $ echo roflcon | sed -r 's/(co)n/\1pter/'
 	roflcopter

Or, the way that I get by accident sometimes (sed without -r):

 	paulproteus at vellum:~ $ echo roflcon | sed 's/(co)n/\1pter/'
 	sed: -e expression #1, char 15: invalid reference \1 on `s' command's RHS
 	bash: echo: write error: Broken pipe

To which I reply:

 	paulproteus at vellum:~ $ Come ON sed, you frickin' knew what I meant!
 	>

You can get the same frustration by using 'grep' instead of 'egrep' (AKA 
'grep -E').

I'm not even going to say how to make this work for sed without -r (which 
is equivalent to grep without -E) since this is a Linux users group, and 
we all use GNU tools here, so let's just ditch the aging past and stop 
remembering the bad old days and extra scary \ characters.

Let's just end the plague of backslashes from the past and do egrep and 
'sed -r'.

-- Asheesh.

-- 
You will overcome the attacks of jealous associates.





More information about the sf-lug mailing list