New to Perl.
I need to parse a report that look like this:
2012-05-29@emaillocalpart@emaildomain@customerid@errormessage@messageid
I used:
my @fields = split(/@/, $line, 6);
Most of the time it works fine, but sometimes the error message will contain an email address and all text after the @ symbol on that email until the end of the string will end on my message id.
I thought about checking for the amount of @s and have a conditional parsing, but is there a better way?
EDIT:
The desired output is a list of strings, with the error message containing whatever came in it (including an occasional email address).
Since there are other applications using the same report I cannot change the separator or escape the output.
Sample lines on the report:
2012-05-29@[email protected]@AB99-5@440 4.4.1 Some error occurred@XYZ35
2012-05-29@[email protected]@ZZ88-6@550 5.1.1 <[email protected]>... User Unknow@GGH93
2012-05-29@[email protected]@YY88-0@550 5.1.1 [email protected] no such user@GGH93
Expected contents of @fields after parsing line 1:
2012-05-29
joedoe
example.com
AB99-5
440 4.4.1 Some error occurred
XYZ35
And after parsing line 2:
2012-05-29
foobar
invalid.com
ZZ88-6
550 5.1.1 <[email protected]>... User Unknow
GGH93
Split ( Split function divide string into substring) /pattern/ (Based on pattern we have split string into substring.), Expression (Expression used to split the string.), Limit (Limit is used to restrict the number of output return using split function in Perl.) Below is the parameter description of above syntax are as follows.
Limit: Limit is an optional parameter that was used in split function in Perl language. Using limit in split function we have restrict the number of output return using split function. How Split Function Works in Perl? Below is the working of split function in perl are as follows.
Split: We have used a pattern, expression name in split function to divide any string into substrings. We have used limit in split function to restrict the number of the splitted output. Pattern: Pattern is used to divide the string into multiple substrings. Based on pattern we have divide string into substring.
Perl string functions. Perl provides a set of functions that allow you to manipulate strings effectively. We cover the most commonly used string functions in the following section for your reference. To find the number of characters in a string, you use the length() function.
If $teststr contains, for example: '2012-05-29@emaillocalpart@emaildomain@customerid@error@me@ssage@messageid';
the following code:
my @fields2=split('@',$teststr);
my @finalfields=@fields2[0 .. 3];
my $finalat=$#fields2-1;
my $errormessage=join('@',@fields2[4 .. $finalat]);
push(@finalfields,$errormessage);
push(@finalfields,$fields2[$#fields2]);
print Data::Dumper->Dump([@finalfields])."\n";
gives the following output:
$VAR1 = '2012-05-29';
$VAR2 = 'emaillocalpart';
$VAR3 = 'emaildomain';
$VAR4 = 'customerid';
$VAR5 = 'error@me@ssage';
$VAR6 = 'messageid';
Apologies - it's rather a verbose solution. You can also do the same in one regular expression:
$teststr=~/(.[^@]*)@(.[^@]*)@(.[^@]*)@(.[^@]*)@(.*)@(.[^@]*)/;
print "$1\n$2\n$3\n$4\n$5\n$6\n";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With