cyzza has asked for the wisdom of the Perl Monks concerning the following question:
Which gets me:my $string = 'to=<adam.clark@ngv.vic.gov.au>, relay=monet1.ngv.vic.gov +.au[10.10.10.20]:25, delay=0.54, delays=0.06/0.02/0/0.46, dsn=2.0.0, +status=sent '; $LogLineHash = qr { ^ ([^=]*)=<?(.*?)>?,?\s+) (.*?) $ }xi; while ( $string ){ print "String: $string\n"; ( $junk , $key , $value, $string ) = split( /$LogLineHash/ , $string ); print "Key: $key\nValue: $value\nLeft Over: $string\n\n"; $Hash{$key}=$value; } while ( my ($key, $value) = each(%Hash) ) { print "$key => $value\n"; }
I was hoping that I could get my regex to dynamically grab all the key value pairs at once with a (?: )+ style grouping such that my regex would be:String: to=<adam.clark@ngv.vic.gov.au>, relay=monet1.ngv.vic.gov.au[10 +.10.10.20]:25, delay=0.54, delays=0.06/0.02/0/0.46, dsn=2.0.0, status +=sent Key: to Value: adam.clark@ngv.vic.gov.au Left Over: relay=monet1.ngv.vic.gov.au[10.10.10.20]:25, delay=0.54, de +lays=0.06/0.02/0/0.46, dsn=2.0.0, status=sent String: relay=monet1.ngv.vic.gov.au[10.10.10.20]:25, delay=0.54, delay +s=0.06/0.02/0/0.46, dsn=2.0.0, status=sent Key: relay Value: monet1.ngv.vic.gov.au[10.10.10.20]:25 Left Over: delay=0.54, delays=0.06/0.02/0/0.46, dsn=2.0.0, status=sent String: delay=0.54, delays=0.06/0.02/0/0.46, dsn=2.0.0, status=sent Key: delay Value: 0.54 Left Over: delays=0.06/0.02/0/0.46, dsn=2.0.0, status=sent String: delays=0.06/0.02/0/0.46, dsn=2.0.0, status=sent Key: delays Value: 0.06/0.02/0/0.46 Left Over: dsn=2.0.0, status=sent String: dsn=2.0.0, status=sent Key: dsn Value: 2.0.0 Left Over: status=sent String: status=sent Key: status Value: sent Left Over: relay => monet1.ngv.vic.gov.au[10.10.10.20]:25 to => adam.clark@ngv.vic.gov.au dsn => 2.0.0 status => sent delay => 0.54 delays => 0.06/0.02/0/0.46
essentially grabbing arbitrary number of key value pairs, but it's not to be as:^(?:([^=]*)=<?(.*?)>?,?\s+)+$
gives me:my @bits = split( /^(?:([^=]*)=<?(.*?)>?,?\s+)+$/ , $string ); foreach ( @bits ){ print "$_\n"; }
which is the last key value pair.status sent
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Adapting parenthesis in regexps
by ikegami (Patriarch) on Mar 29, 2007 at 12:59 UTC | |
|
Re: Adapting parenthesis in regexps
by Fletch (Bishop) on Mar 29, 2007 at 12:54 UTC | |
|
Re: Adapting parenthesis in regexps
by Ionitor (Scribe) on Mar 29, 2007 at 13:05 UTC | |
by Ionitor (Scribe) on Mar 29, 2007 at 13:55 UTC | |
|
Re: Adapting parenthesis in regexps
by bobf (Monsignor) on Mar 29, 2007 at 14:48 UTC |