in reply to Re: Stop runaway regex # alarm , /g , (?{code})
in thread Stop runaway regex

> But I am not interested to test it for you :)

Well curiosity won!

return causes an error and "won't stay shared" lexical variables are problematic too.

Though this works and can be extended:

use strict; use warnings; my $start; my $diff; my $timeout; sub tst { $timeout=shift; my $str = "a"x10000; $str .= "b"; $start = time; $str =~ /^ (( a* (?{ $diff= time-$start; die "stopped after $diff sec" if $diff >=$timeout; }) )*)* $/x; } tst(10);

output:

Complex regular subexpression recursion limit (32766) exceeded at time +out_regex.pl line 15. stopped after 10 sec at (re_eval 1) line 3.

Cheers Rolf

( addicted to the Perl Programming Language)

update

NB: this approach considerably slows down the regex when done in the innermost group-loop, outer loops OTOH might be checked too seldom. YMMV

Replies are listed 'Best First'.
Re^3: Stop runaway regex # (?{code})
by Anonymous Monk on May 29, 2014 at 20:08 UTC

    return causes an error and "won't stay shared" lexical variables are problematic too.

    Get newer perl (latest), this part upgraded

    In the meantime, local our $diff; ...