Bug #12103
closedruby process hangs while executing regular expression.
Description
The following code hangs
regex = /(\((\w+([\p{Punct}\s]{,3}\w*)*)\))/i
detail = "(said companies being fictitous sellers"
detail =~ regex
However when I change {,3} to {1,3}, the code executes and moves on. Otherwise hangs
Updated by duerst (Martin Dürst) about 9 years ago
- Status changed from Open to Rejected
I simplified this as follows:
regex = /(a+( ?a*))/
detail = "(aaaa aaaaaaa aaaaa aaaaaaaaa aaaaa"
It still hangs. But it's easier to see why: The part (a+( ?a)* allows many different ways to match the same thing, and all these are tried. For more background, please see http://www.regular-expressions.info/catastrophic.html.
Updated by normalperson (Eric Wong) about 9 years ago
duerst@it.aoyama.ac.jp wrote:
Status changed from Open to Rejected
I simplified this as follows:
regex = /(a+( ?a*)*)/
detail = "(aaaa aaaaaaa aaaaa aaaaaaaaa aaaaa"
Keep in mind the equivalent Perl terminates just fine:
my $detail = "(aaaa aaaaaaa aaaaa aaaaaaaaa aaaaa";
$detail =~ /\(a+( ?a*)*\)/;
Tested on an old perl v5.14.2 from Debian wheezy (oldstable).
So I prefer we acknowledge cases like these as bugs to at least
encourage someone to fix them.
Not a high priority for me, though, as I'm not familiar with
the inner workings of RE engines (and also know to avoid tricky REs).
Updated by shyouhei (Shyouhei Urabe) about 9 years ago
On 02/28/2016 12:22 PM, Eric Wong wrote:
Keep in mind the equivalent Perl terminates just fine:
Yes. But,
So I prefer we acknowledge cases like these as bugs to at least
encourage someone to fix them.
Ruby's (and Perl's) RE grammar is known to be NP complete. Fixing this ``issue'' ultimately requires a proof of P=NP. I don't think it'll happen in a near future.
I guess Perl's RE engine can happen to handle this specific /(a+( ?a*)*)/ case, maybe due to their optimization.