Paul Lalli wrote:
On Aug 9, 10:07 am, [EMAIL PROTECTED] (Oryann9) wrote:
Paul, what college?
Rensselaer Polytechnic Institute, Troy, NY
http://www.cs.rpi.edu/~lallip/perl/spring07
Also on your page at:
http://cgi2.cs.rpi.edu/~lallip/perl/spring07/regexp_notes.shtml
you say:
^.* and .*$
Some students chose to anchor their patterns to the start or end of
the string, but then put the "any text" pattern token right next to
them. This is pointless. Saying "beginning of string, then anything,
THEN my pattern" is no different than saying "my pattern"; "beginning
of string, then anything" is vacuously true, for every string, always.
The statement 'Saying "beginning of string, then anything, THEN my pattern" is
no different than saying "my pattern"' is wrong because '.*' is greedy so
putting '.*' in front of your pattern will move to the end of the string and
backtrack until it finds the last pattern in the string while using pattern
alone will always find the first pattern in the string.
$ perl -le'
#use warnings;
use strict;
my $string = q[ pattern1 pattern2 pattern3 ];
for my $regex ( qr/(pattern\d+)/, qr/.*(pattern\d+)/, qr/^.*(pattern\d+)/ ) {
print $1 if $string =~ /$regex/;
}
'
pattern1
pattern3
pattern3
If you can guarantee that your pattern will only be found once then yes, there
is little difference.
If the only purpose is to confirm that the pattern exists then the
backtracking pattern will *probably* be less efficient.
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/