Just in case you hadn't come across these before:

1.  Check out perldoc perlre and, since it is a bit hard to understand, so
check out other references on regular expressions where necessary.  Regular
expressions are a vast and complicated subject when you first start to learn
them, but it looks like you've got some of the basics down.

2.  In the example given by Janek, the part of the match that lies inside
the parentheses is automatically assigned to the built-in variable $1.  $1
will retain this value until another successful match is made which
overwrites it.  Perl will continue to assign numeric variables if you use
multiple parentheses.  Thus:

my $string = "My dog has fleas.";
$string =~ /(dog).+(flea)/; #$1 is now dog $2 is now flea

3.  This part took me a while to understand at first.  The result of the
match above is a list of the matches, so:

  my @pets = ($string =~ /(dog).+(flea)/);
    #() around match keeps it in list context
 
  my $numberOfPets = $string =~ /(dog.+(flea)/;
    #in scalar context returns the number of items

    #@pets is now ("dog","flea")
    #$numberOfPets is now 2



-----Original Message-----
From: Janek Schleicher [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, July 03, 2002 3:10 PM
To: [EMAIL PROTECTED]
Subject: Re: extracting text


Charlie Farinella wrote at Wed, 03 Jul 2002 23:51:44 +0200:

> I have the following script that prints email addresses enclosed in <>
from a logfile.  It works
> by removing everything up to and including the bracket on the left, and
then doing the same on the
> right.  I would like to be able to just extract the text between the
brackets.
> 
> I have been unable to figure out how to do this and am hoping for some
help.
> 
> ===============================
>  #!/usr/local/bin/perl -w
> 
> # Read through the maillog, and print out the email # addresses.
(<email.address>)
> 
> open (INFILE, "<$ARGV[0]" );
> 
> while (<INFILE>) {
> 
>         if( $_ =~ /<.*>/i ) {
>                 $_ =~ s/^.*<//g;
>                 $_ =~ s/>.*$//g;
>                 print $_;
>         }
>         }
>         }
> close INFILE;
> 

while (<INFILE>) {
   /<(.*?)>/ and print $1;
} 


Best Wishes,
Janek

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to