From: "nathanst (Nathan Stratton Treadway)" Date: 2012-06-01T14:31:30+09:00 Subject: [ruby-core:45359] [ruby-trunk - Bug #6525][Open] misleading example in Permformance section of Regexp documentation? Issue #6525 has been reported by nathanst (Nathan Stratton Treadway). ---------------------------------------- Bug #6525: misleading example in Permformance section of Regexp documentation? https://bugs.ruby-lang.org/issues/6525 Author: nathanst (Nathan Stratton Treadway) Status: Open Priority: Normal Assignee: Category: Target version: ruby -v: 1.9.3 In the Performance section of the Regexp documention, it is stated that changing from Regexp.new('a?' * 29 + 'a' * 29) to Regexp.new('\A' 'a?' * 29 + 'a' * 29) speeds up the match significantly. However, '\A' 'a?' * 29 actually expands to "\\Aa?\\Aa?....\\Aa?" -- which doesn't seem like it's what you'd actually want, since [for example] Regexp.new('\A' 'a?' * 29 + 'a' * 29).match('a' * 50) will only match 30 'a' characters [the 29 from 'a' * 29, plus one single one from the set of '\Aa?' items. [That is, Regexp.new('\A' 'a?' * 29 + ...) is doesn't match any more 'a' characters than Regexp.new('\A' 'a?' + ...) would.] One can get around this by changing the expression to Regexp.new('\A' + 'a?' * 29 + 'a' * 29), in which case additional "a" characters do get matched as the string gets longer... but with that Regexp the .match('a' * 29) still takes about the same amount of time as the original one. So I wonder if there is some better example that could be used here? Nathan -- http://bugs.ruby-lang.org/