Project

General

Profile

Actions

Bug #7787

closed

ruby 1.9 regexp quantifiers behave unpredictably when stacked

Added by calfeld (Christopher Alfeld) about 11 years ago. Updated over 5 years ago.

Status:
Rejected
Target version:
-
ruby -v:
ruby 1.9.3p327 (2012-11-10 revision 37606) [x86_64-darwin12]
[ruby-core:51879]

Description

Ruby (1.8 and 1.9) allows for stacked quantifiers such as /x{2}{5}/ and appears to treat them more or less as expected, i.e., modulo capturing, /x{2}{5}/ is equivalent to /(x{2}){5}/ is equivalent to /x{10}/.

However, in Ruby 1.9, such stacking quantifiers can lead to extreme search time and in ways that are difficult to predict.

"x"*1000 =~ /x{2}{5}{8}{3}/     # runs instantly
"x"*1000 =~ /x{2}{5}+{8}{3}/  # runs for an unknown but long time
"x"*1000 =~ /x{2}{5}+{8}/      # runs instantly
"x"*1000 =~ /x{2}{5}+{7}{3}/  # runs instantly
"x"*1000 =~ /x{2}{5}+{7}{3} / # runs for an unknown but long time

All of the above run instantly in Ruby 1.8. Note that adding parenthesis does not change the runtime.

I realize this might not be considered a bug and will take no offense if it as closed as not-a-bug. But it is surprising, and I hope is of interest.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0