regex - Selectively splitting a string in Perl -
new perl.
i need parse report this:
2012-05-29@emaillocalpart@emaildomain@customerid@errormessage@messageid i used:
my @fields = split(/@/, $line, 6); most of time works fine, error message contain email address , text after @ symbol on email until end of string end on message id.
i thought checking amount of @s , have conditional parsing, there better way?
edit:
the desired output list of strings, error message containing whatever came in (including occasional email address).
since there other applications using same report cannot change separator or escape output.
sample lines on report:
2012-05-29@joedoe@example.com@ab99-5@440 4.4.1 error occurred@xyz35 2012-05-29@foobar@invalid.com@zz88-6@550 5.1.1 <foobar@invalid.com>... user unknow@ggh93 2012-05-29@barfoo@invalid.com@yy88-0@550 5.1.1 barfoo@invalid.com no such user@ggh93 expected contents of @fields after parsing line 1:
2012-05-29 joedoe example.com ab99-5 440 4.4.1 error occurred xyz35 and after parsing line 2:
2012-05-29 foobar invalid.com zz88-6 550 5.1.1 <foobar@invalid.com>... user unknow ggh93
similar daxim's answer, way of writing it:
my $re = '^' . '([^@]*)@'x4 . '(.*)@([^@]*)$'; @fields = $line =~ /$re/; you may want error checking here:
my @fields = $line =~ /$re/ or die "can't parse '$line'";
Comments
Post a Comment