[clue-tech] Re: regex smarties - what am I doing wrong?

Patrick H. clue at feystorm.net
Wed Sep 3 23:36:56 MDT 2008


Aye, I hate doing anything more than a very simple regex in anything but 
perl. Perl's regex is easy, sensible, and powerful. Sure using perl is 
kinda like using a sledgehammer to push in a thumbtack, but its better 
than using your thumb to push in a nail (which is what sed and awk feel 
like sometimes).

perl -ne 'print "$1\n" if $_ =~ /((?:\d{1,3}\.){3}\d{1,3})/' /etc/hosts

^^much cleaner than that sed mess. Think someone gave something similar 
earlier, but its buried in my inbox...

-------- Original Message  --------
Subject: Re: [clue-tech] Re: regex smarties - what am I doing wrong?
From: David L. Anselmi <anselmi at anselmi.us>
To: CLUE tech <clue-tech at cluedenver.org>
Date: Wednesday, September 03, 2008 6:45:54 PM
> David L. Willson wrote:
>> $ echo -e "98.76.54.32 Alice\nBob 123.45.67.89\nCarol or Dave 
>> 064.032.016.008\n" \
>>> | sed -e 's/\(\([[:digit:]]\{1,3\}\.\)\{3\}[[:digit:]]\{1,3\}\).*/\1/'
>> 98.76.54.32
>> Bob 123.45.67.89
>> Carol or Dave 064.032.016.008
>>
>> As you can see, the Alice line is properly trimmed, but not the other 
>> lines.  Now, I
>> have two questions:  Why don't the single quotes preserve the sed 
>> command properly
>> without all the escaping of the special chars?  And, why doesn't the 
>> second close-paren
>> close the pattern-capture?  Regexes are hard...
>
> You only want \(...\) where you're doing a capture.  If you're doing 
> grouping just use (...).  The single quotes do preserve the command 
> for sed.  You aren't using \( because the shell interprets ( but 
> rather because \( and ( are different parts of a regex.  But there are 
> lots of variations.
>
> I think perl's regexes are defined the most clearly so I'd use that. 
> Otherwise I typically use awk vs grep | sed.
>
> I think your stated requirements already justify perl, and you'll wind 
> up switching to it if you add anything to them.
>
> Dave
>
> _______________________________________________
> clue-tech mailing list
> clue-tech at cluedenver.org
> http://www.cluedenver.org/mailman/listinfo/clue-tech


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://cluedenver.org/pipermail/clue-tech/attachments/20080903/d610fb1c/attachment-0001.html


More information about the clue-tech mailing list