[thelist] pattern matching mail in perl

Lisa Crenshaw licren at unx.sas.com
Fri Nov 10 09:36:28 CST 2000


This from a perl tutorial seems like a pretty good basic introduction
to perl's pattern matching : 
   http://cres20.anu.edu.au/manuals/perl/matching.html

and here are the gory details on regular expressions from www.perl.com:
   http://www.perl.com/pub/doc/manual/html/pod/perlre.html

Use parentheses to remember parts of a line you've just matched. For
example, assuming that $_ contains one line of your mail file :

$_ =~ /Invoice Number: (.+)/ ;
my $anything_after_invoice = $1;

$anything_after_invoice will now be all characters (except newlines) 
after the Invoice Number. Similarly:

$_ =~ /Invoice Number: (\d+)/ ;
my $digits = $1;	### contains any digits immediately following 

  or 

$_ =~ /Invoice Number: ([\s\d]+)/ ;
my $digits_and_whitespace  = $1;  ### contains digits and whitespace
				  ### immediately following 

Good luck. 

--lisa

Lisa Crenshaw
Webmaster/ Sysadmin
SAS Institute
lisa.crenshaw at sas.com

On Fri, 10 Nov 2000, Adrian Fischer wrote:

> Well I guess you ve caught on by now....I haven't a clue what I'm doing
> here!
> 
> At last I can get access to the .mail file on the linux server that hosts
> me.  If its sin a useable format is yet to be seen.  At present I can dump
> the whole thing to screen, but that's not what I want, it just proves to me
> that I've accessed it.
> This is how I'm doing it:
> <snip>
> death('didn't work') unless (open(MAILFILE, "$config{'pathtomail'}.mail"));
>  (@junk) = <MAILFILE>;
> close MAILFILE;
>   print "@junk
> </snip>
> 
> the print just dumps the whole file (possibly many emails) to the screen, no
> formatting.  It doesn't need to be formatted.  I don't want it displayed on
> the screen.  I want to be able to search the file and find occurrences of
> certain key words and then see whets on the right of them.  For instance.
> Find occurrences of the word "Invoice Number:" and return the number to the
> right of it. There may be many instances of the word "Invoice Number:" and I
> need to have them all returned.
> 
> I think part of my problem is that the file is not delimited by anything.  I
> know if I do this:
> <snip>
> death('it don't work') unless (open(MAILFILE,
> "$config{'pathtomail'}.mail"));
>  ($crap,$snap,$pop,$q1,$q2,$q3,$q4......etc) = <MAILFILE>;
> close MAILFILE;
>   foreach ($crap)
>  {
> print "$crap...$snap...$pop...$q1...$q2...$q3...$q4....................etc";
>  }
> </snip>
> it seems to print a line at a time so perhaps there is a line feed at the
> end of each row (obviously) and each variable is grabbing a line.  That's a
> step in the right direction.  But I don't know how many emails may be in the
> file at a given time so that doesn't really help.
> 
> Even a line at a time ( If I could somehow do a while loop and grab and test
> each line as it reads the file might work) might be to much.  Perhaps if I
> could grab each individual word and test it against my search word.
> 
> And there in lays the problem.  I know what I need to be doing but don't
> know how to do it.  My reference talks about pattern matching but does not
> give an example that I can play with.  I've searched online resources for
> examples but found nothing.
> 
> Once I've grabbed the variables I need from the mail the rest is
> easy...update my db and away we go.  But it could all be for naught if I
> cant get this sucker to work.
> 
> Any online resources you know of that are in big black bold print (with
> pictures would help).  Examples are what I really need.  I've managed to get
> this far by playing with examples and chopping and changing them.
> 
> Could be a big ask on a Friday (its actually 1045 pm Friday night as I write
> this but I'm hoping someone on the other side of the world has nothing
> better to do on their Friday.;-))
> 
> Sorry to hassle you all so many times in one day...
> 
> 
> Regards
> 
> (a very battle weary)
> Adrian Fischer
> Brisbane
> Australia
> 
> 
> ---------------------------------------
> For unsubscribe and other options, including
> the Tip Harvester and archive of TheList go to:
> http://lists.evolt.org Workers of the Web, evolt ! 
> 







More information about the thelist mailing list