|
Chas. Owens |
at Jun 22, 2009 at 11:45 am
|
⇧ |
| |
On Jun 21, 2009, at 10:19, "[email protected]" wrote:On Sun, 2009-06-21 at 07:16 -0700, John W. Krahn wrote:EASY buzzhost.co.uk wrote:
OK, I'm an idiot. I just cannot get a simple regex match to work as
intended. I've google, I dug out my books of regex and Perl but I
hang
my head in shame and ask here for help;
Take This line;
Jun 14 08:14:41 pingpong postfix/smtpd[22386]: NOQUEUE: reject: RCPT
from host120-109-dynamic.56-82-r.retail.telecomitalia.it
[82.56.109.120]:
554 5.7.1 <
[email protected]>: Sender address rejected: spoofing of
domain; from=<
[email protected]> to=<
[email protected]> proto=SMTP
helo=<82.56.109.120>
I'm trying to grab the email address in the 'from=<...' portion. I
can
pin this down with look ahead look behind;
(?(?=>)
But it has a side effect of going to the last > on the line, not the
closing half of the pair <>.
To knock out the complications I drop to a simple pattern without
the
look ahead/behind;
<.+\@.+>
again it matches all the way to the end of the line, not just the
opening < and closing > as intended.
Now, a little research and I come up with 'greedy -v- non greedy'
matching and thought I was on to something. But I can get that to
play
ball either;
(.*?)>
But No dice. After 8 hours on this I'm starting to kick myself
blue. Can
anyone put me out of my misery?
You need to make both sides of @ non-greedy:
(?(?=>)
Or:
<.+?\@.+?>
John
--
Those people who think they know everything are a great
annoyance to those of us who do. -- Isaac Asimov
John. I love you. I want to have your Babies. Thank you soo much!
I don't understand WHY I have to do that, but it works and at this
moment that is good enough for me!
THANK YOU SIR!
--
To unsubscribe, e-mail:
[email protected]For additional commands, e-mail:
[email protected]http://learn.perl.org/ It is easier to see why with a shorter example:
#!/usr/bin/perl
use strict;
use warnings;
my $s = "123456";
print
"greedy:\n", (map { "\t$_\n" } $s =~ /(.+)(.)(.+)/),
"partial:\n", (map { "\t$_\n" } $s =~ /(.+?)(.)(.+)/),
"non greedy:\n", (map { "\t$_\n" } $s =~ /(.+?)(.)(.+?)/);
The non-greedy modifier (?) causes the regex engine to look for the
shortest string (starting from the current position) that matches.
So, when the match is greedy, the first period consumed all but the
last two characters (because otherwise it would not have matched).
When the first period is made non-greedy, it consumes one character,
then the second period matches, and, finally, the third consumes the
rest. In the last example all three match just one character.