[EMAIL PROTECTED] wrote on 04/06/2004 12:01:28 PM:
> I have a pattern matching problem that isn't strictly VMS related
(although I
> amd running on VMS perl).
>
> I want to make a perl regexp that will match an @ starting at the
beginning of
> the string followed by any characters. (Actually I lied. The pattern I
want to
> ultimately match is a bit more complicated but I narrowed the fact that
it won't
> match as I expect down to this problem.) Anyway I thought that it would
be this:
>
> /[EMAIL PROTECTED]/
>
> or maybe this:
>
> /[EMAIL PROTECTED]/
.* means zero or more of any character and might be avoided depending on
your
needs (e.g. would /[EMAIL PROTECTED]/ or /[EMAIL PROTECTED]/ better meet your needs?).
> but I can't get it to match. Can anyone explain the behavior of the
following
> program: Why don't all the matches match?
You have not assigned the test data in the manner that you apparently
expect.
You might want to print out $toline to help diagnose the problem further.
I note that you apparently never assigned the @T array for example.
> LASSIE> ty z.pl
> $toline = "@";
> $match = $toline =~ /^\@/;
> print "\$match is $match\n";
>
> $toline = "@T";
> $match = $toline =~ /[EMAIL PROTECTED]/;
> print "\$match is $match\n";
>
> $match = $toline =~ /[EMAIL PROTECTED]/;
> print "\$match is $match\n";
>
> $match = $toline =~ /[EMAIL PROTECTED]/;
> print "\$match is $match\n";
Interestingly, if you run your test program under perl 5.005_02 you'll
see a useful warning:
$ perl z.pl
In string, @T now must be written as [EMAIL PROTECTED] at z.pl line 8, near "@T"
Execution of z.pl aborted due to compilation errors.
%SYSTEM-F-ABORT, abort
If I re-write your program like so (note the use of single quotation
marks):
$ type z_1.pl
$toline = '@';
$match = $toline =~ /^\@/;
print "\$match is $match\n";
$toline = '@T';
$match = $toline =~ /[EMAIL PROTECTED]/;
print "\$match is $match\n";
$match = $toline =~ /[EMAIL PROTECTED]/;
print "\$match is $match\n";
$match = $toline =~ /[EMAIL PROTECTED]/;
print "\$match is $match\n";
I obtain runs like so under either perl 5.005_02 or perl 5.8.1:
$match is 1
$match is 1
$match is 1
$match is 1
Another useful trick for quickly evaluating regular expression testing
would be to put literal data in a <DATA> block after an __END__
like so:
$ type z_2.pl
while (<DATA>) {
chomp;
print "$_\n";
$toline = $_;
$match = $toline =~ /^\@/;
print "\$match is $match\n";
$match = $toline =~ /[EMAIL PROTECTED]/;
print "\$match is $match\n";
$match = $toline =~ /[EMAIL PROTECTED]/;
print "\$match is $match\n";
$match = $toline =~ /[EMAIL PROTECTED]/;
print "\$match is $match\n";
}
exit;
__END__
@
@T
And here too the runs are the same under perl 5.005_02 and perl 5.8.1.
E.g.:
$ perl z_2.pl
@
$match is 1
$match is
$match is 1
$match is
@T
$match is 1
$match is 1
$match is 1
$match is 1
I hope that helps.
Peter Prymmer