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); #### Complex regular subexpression recursion limit (32766) exceeded at timeout_regex.pl line 15. stopped after 10 sec at (re_eval 1) line 3.