string ve stringu
Lukas Svoboda
xsvobod4 na aurora.fi.muni.cz
Čtvrtek Červen 19 09:24:36 MEST 2003
On Thu, Jun 19, 2003 at 08:04:19AM +0200, Ladislav Vaiz wrote:
> > > $a = "abc|uvxy|123456|defgh";
> > > a pak uz jen staci
> > > print "ANO" if ($b =~ /$a/o);
> > Jo, tohle je chytryyy :-) Diky.
> >
>
> Co presne znamena /o ? www.perldoc.com/perl5.8.0/pod/perlre.html mlci,
> z tutorialu mi pripadne, ze se jedna o optimalizaci, kdy se regexp nemeni
> a tudiz staci zkompilovat vyraz pouze jednou. Je to tak?
viz. Perl Cookbook:
6.10. Speeding Up Interpolated Matches
Problem
You want your function or program to take one or more regular expressions as
arguments, but doing so seems to run slower than using literals.
Solution
To overcome this bottleneck, if you have only one pattern whose value won't
change during the entire run of a program, store it in a string and use
/$pattern/o.
while ($line = <>) {
if ($line =~ /$pattern/o) {
# do something
}
}
If you have more than one pattern, however, that won't work. Use one of the
three techniques outlined in the Discussion for a speed-up of an order of
magnitude or so.
...
What you really need is some way to get Perl to compile each pattern once and
let you directly refer to the compiled form later on. Such functionality is
directly supported in the 5.005 release in the form of a qr//
regular-expression quoting operator. For prior releases, that's exactly what
the experimental Regexp module from CPAN was designed for. Objects created by
this module represent compiled regular expression patterns. Using the match
method on these objects matches the pattern against the string argument.
Methods in the class exist for extracting backreferences, determining where
pattern matched, and passing flags corresponding to modifiers like /i.
Proste receno, regularni vyraz se kompiluje na FSA, kterym se pak prochazi
pri vyhledavani. Pokud opakovane vyhodnocujes/volas ten samy regexp.
v kterem je promenna, perl musi vzdy tento regexp kompilovat. Pokud vis, ze
se ale pomenna nemeni, je tato kompilace zbytec. Pak pis /o. A pokud
pouzivas vice jak jeden regexp. pis qr//.
Lukas
Další informace o konferenci Perl