On Sat, Dec 13, 2008 at 19:35, explor <[email protected]> wrote: > Hi Gurus, > I am new to perl and need some help to learn regex in perl. From the > below line i need to extract following: > > Part I > 1) $ENVFROM = [email protected] > 2) $ENVTO1 = [email protected] > 3) $ENVTO2 = [email protected] > 4) $ENVTO3 = [email protected] > > > line=EnvFrom: [email protected], HdrTo: <[email protected]>, > EnvTo: [email protected], [email protected], [email protected], Subject: > TEST for perl snip
If you can guarantee that EnvFrom:, EnvTo:, and Subject: will always exist and that they will exist in the same order you can say #!/usr/bin/perl use warnings; use strict; my $line = 'line=EnvFrom: [email protected], HdrTo: <[email protected]>, EnvTo: [email protected], [email protected], [email protected], Subject: TEST for perl'; #assign captures from regex to $from and $to my ($from, $to) = $line =~ m{ EnvFrom: #match starting at EnvFrom: \s+ #follwed by one or more whitespace characters ( #begin first capture [^\s,]+ #match one or more of any chacter but whitespace or , ) #end first capture .* #match the largest string up to next anchor EnvTo: #which is the string EnvTo: \s+ #follwed by one or more whitespace characters ( #begin second capture .*? #match the shortest string up to the next anchor ) #begin second capture Subject: #which is the string Subject: }xms; #all of the addresses are still in one string for EnvTo:, so we #split the on a comma followed by zero or more whitespace characters #and store the resulting list into an array for easy access my @to = split /,\s*/, $to; print "from: $from\nto: ", join(", ", @to), "\n"; > > Part II > We have a huge log files with thousands of entries like above so might > need to be done in either while /for loop in the final phase. Also, > the EnvTo: can be from one email address to multiple. In the above > example there are 3 EnvTo but it can be more than 3 or can be one only > but the regex should be able to determine and put each in a scalar > variable. > > I do need the regex specific to Part I (This will help me learn the > regex better) and also separate regex for Part II if possible. If each record is on a line by itself, there is no need for a separate regex: #!/usr/bin/perl use warnings; use strict; #read lines from either STDIN or a set of files passed in on the commandline while (my $line = <>) { #move to the next line if the regex doesn't match next unless my ($from, $to) = $line =~ m{ EnvFrom: \s+ ([^\s,]+) .* EnvTo: \s+ (.*?) Subject: }xms; my @to = split /,\s*/, $to; print "for record $. I found\n\tfrom: $from\n\tto: ", join(", ", @to), "\n"; } If each record is split over many lines you will need a different solution depending on how the data is split. If you post what your data looks like we may be able to help. -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected] http://learn.perl.org/
