<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
<title></title>
</head>
<body bgcolor="#ffffff" text="#0050d0">
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).<br>
<br>
perl -ne 'print "$1\n" if $_ =~ /((?:\d{1,3}\.){3}\d{1,3})/' /etc/hosts<br>
<br>
^^much cleaner than that sed mess. Think someone gave something similar
earlier, but its buried in my inbox...<br>
<br>
-------- Original Message --------<br>
Subject: Re: [clue-tech] Re: regex smarties - what am I doing wrong?<br>
From: David L. Anselmi <a class="moz-txt-link-rfc2396E" href="mailto:anselmi@anselmi.us"><anselmi@anselmi.us></a><br>
To: CLUE tech <a class="moz-txt-link-rfc2396E" href="mailto:clue-tech@cluedenver.org"><clue-tech@cluedenver.org></a><br>
Date: Wednesday, September 03, 2008 6:45:54 PM<br>
<blockquote cite="mid:48BF2FC2.80908@anselmi.us" type="cite">David L.
Willson wrote:
<br>
<blockquote type="cite">$ echo -e "98.76.54.32 Alice\nBob
123.45.67.89\nCarol or Dave 064.032.016.008\n" \
<br>
<blockquote type="cite">| sed -e
's/\(\([[:digit:]]\{1,3\}\.\)\{3\}[[:digit:]]\{1,3\}\).*/\1/'
<br>
</blockquote>
98.76.54.32
<br>
Bob 123.45.67.89
<br>
Carol or Dave 064.032.016.008
<br>
<br>
As you can see, the Alice line is properly trimmed, but not the other
lines. Now, I
<br>
have two questions: Why don't the single quotes preserve the sed
command properly
<br>
without all the escaping of the special chars? And, why doesn't the
second close-paren
<br>
close the pattern-capture? Regexes are hard...
<br>
</blockquote>
<br>
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.
<br>
<br>
I think perl's regexes are defined the most clearly so I'd use that.
Otherwise I typically use awk vs grep | sed.
<br>
<br>
I think your stated requirements already justify perl, and you'll wind
up switching to it if you add anything to them.
<br>
<br>
Dave
<br>
<br>
_______________________________________________
<br>
clue-tech mailing list
<br>
<a class="moz-txt-link-abbreviated" href="mailto:clue-tech@cluedenver.org">clue-tech@cluedenver.org</a>
<br>
<a class="moz-txt-link-freetext" href="http://www.cluedenver.org/mailman/listinfo/clue-tech">http://www.cluedenver.org/mailman/listinfo/clue-tech</a>
<br>
</blockquote>
<br>
<br>
</body>
</html>