in reply to Stop runaway regex

> is there some form of timer that I can use to terminate a regex that takes longer than X seconds to complete

No! But you might want to experiment with alarm and stop the whole program with die².

> I know it can be modified so it doesn't break, etc,

while I don't like the way you are asking, I'll give you two general hints:

Cheers Rolf

( addicted to the Perl Programming Language)

¹) I said die cause I've never seen any return-like exit from a regex ...which doesn't mean it's impossible² (?). ( But I am not interested to test it for you :)

update

²) well you can always catch die from within eval { BLOCK }

Replies are listed 'Best First'.
Re^2: Stop runaway regex # (?{code})
by LanX (Saint) on May 29, 2014 at 16:58 UTC
    > 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

      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; ...

Re^2: Stop runaway regex # alarm problematic
by LanX (Saint) on May 29, 2014 at 18:16 UTC
    Well, while the docs look promising, alarm didn't interrupt regexes for me!

    Signals are executed only after the regex is completed. (5.10 / Linux)

    Found this thread "Timeout alarm for regex", so this always used to be problematic!

    (see also Deferred Signals (Safe Signals))

    No idea if it's still the case for newer Perl versions.

    Cheers Rolf

    ( addicted to the Perl Programming Language)