in reply to Parsing a csv file from Exchange

Others have provided specific re's to extract the SMTP address, here is a more generic solution which will grab all the fields into a structure-
while (<DATA>){ chomp; my %rec=(); while ( / ([^:]*) # any amount of non ":"'s -better as ([A-Z0-9]*)? : # followed by a colon ([^%]*) # any amount of non "%"'s %{0,1} # followed by 0 or 1 % characters /xg ){ $rec{$1}=$2; } print $rec{SMTP}, "\n"; # for example } __DATA__ SMTP:makeusbetter@domain.com%X400:c=US;a= ;p=domain ;o=domain;s=makeus +better;%CCMAIL:makeusbetter at domain% CCMAIL:Hunter, Sandy at domain%MS:domainin/domain/sandy%SMTP:sandyh@do +main.com%X400:c=US;a= ;p=domain

The second line of your sample data did not end with an % which I suspect it normally would so I made the % char optional, I think for your actual data you could change the re to /([^:]*):([^%]*)%/g

--
my $chainsaw = 'Perl';

Replies are listed 'Best First'.
(jeffa) 2Re: Parsing a csv file from Exchange
by jeffa (Bishop) on May 21, 2002 at 14:30 UTC
    Very nice, but instead of throwing away the hash each loop, why not store it in an array?
    use strict; use Data::Dumper; my @rec; while (<DATA>){ chomp; my %rec; $rec{$1}=$2 while /([^:]*):([^%]*)%{0,1}/xg; push @rec,{%rec}; } print Dumper \@rec; print $rec[0]->{SMTP}, "\n"; print $rec[1]->{SMTP}, "\n";

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)