develooper Front page | perl.perl5.porters | Postings from May 2004

Re: slow regex?

Thread Previous | Thread Next
From:
Yitzchak Scott-Thoennes
Date:
May 24, 2004 22:11
Subject:
Re: slow regex?
Message ID:
20040525051112.GA2796@efn.org
On Tue, May 25, 2004 at 12:23:01AM +0100, Dave Mitchell <davem@iabyn.com> wrote:
> This regex takes 2.5 secs on my machine to fail to match (5.8.4 and
> bleed):
> 
>     $_ = <<'EOF';
>     AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
>     BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
>     EOF
> 
>     print "matched\n" if /.*?A.*?B+\s+X/s;
> 
> While I can see that it may need to backtrack 70x70 times, this still
> seems quite slow. (This is a stripped-down version of a regex I wrote
> for production code that basically  ground to a halt).
> 
> Is this is bug, or do I have unrealistic expectations?
> 
> Dave.

Any regex with a series of * or + can be bad news (especially if it
is going to end up failing).  Use (?> ) to surround parts you know
won't need to backtrack.  See perlre.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About