Am�rico Albuquerque wrote:
> Hi
>
> I have a config file that can have three formats:
> "[alpha] [alpha]", "[alpha]: [alpha]" or "[alpha] *[alpha]"
>
> So I made 3 regexp to check each variation:
> $line1 =~ /\s*([\w-_\.]+)\s+([\w-_\.]+)/;
> $line2 =~ /\s*([\w-_\.]+):\s*([\w-_\.]+)/;
> $line3 =~ /\s*([\w-_\.]+)\s+\*([\w-_\.]+)/;
>
> the problem is that $1 and $2 keep the falues of the previous match.
> That means that I can't see if a match was foud unless I have to test
> it for each line.
> Is there a way to clear the matched subexpressions of the $1...$9
> variables or do I have to check each line to see what test applies?
You should always check whether a match was successful before attempting to use
$1, $2 etc.
Also, \w includes underscore (_), and '.' does not need to be escaped in a
character class.
Also you regexes can be easier to understand if you break them up, e.g.
-----------------------------------------------------
use strict;
use warnings;
my $wordRE = qr{[\w.-]+};
my $form1RE = qr{($wordRE)\s+($wordRE)};
my $form2RE = qr{($wordRE):\s+($wordRE)};
my $form3RE = qr{($wordRE)\s+\*($wordRE)};
while (<DATA>) {
chomp;
if (/$form1RE/ || /$form2RE/ || /$form3RE/) {
print "word1='$1' word2='$2'\n";
}
else {
print "Nope '$_'\n";
}
}
__DATA__
fred jim
hello-sid: wotcha.cock
you *me
harry: *marvin
-----------------------------------------------------
HTH
-----------------------------------------------------------------------
The information contained in this e-mail is confidential and solely
for the intended addressee(s). Unauthorised reproduction, disclosure,
modification, and/or distribution of this email may be unlawful. If you
have received this email in error, please notify the sender immediately
and delete it from your system. The views expressed in this message
do not necessarily reflect those of LIFFE Holdings Plc or any of its subsidiary
companies.
-----------------------------------------------------------------------
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs