Jeff Schwab
6/12/2006 11:58:00 PM
Robert Klemme wrote:
> Jeffrey Schwab wrote:
>> Robert Klemme wrote:
>>> Bit Twiddler wrote:
>>>> "Jeffrey Schwab" <jeff@schwabcenter.com> wrote in message
>>>> news:vTKhg.15710$Qg.11779@tornado.southeast.rr.com...
>>>>> Bit Twiddler wrote:
>>>>>>> if my_variable =~
>>>>>>> /^(?:some|soome|save|catch|blarg|safe|wrest|template)/i
>>>>>> Why is the "?:" required?
>>>>> Because the OP asked that the statement be written "efficiently."
>>>>> Text between parentheses ordinarily is captured, i.e. stored in a
>>>>> special variable, after each match. The ?: tells the regular
>>>>> expression parser that the parentheses are being used only for
>>>>> grouping, and thereby avoids the overhead of capturing text.
>>>
>>> When talking about efficiency then the pattern can be made even
>>> better (manual, probably incomplete optimization):
>>>
>>> /^(?:s(?:o(?:me|ome)|a(?:ve|fe))|catch|blarg|wrest|template)/i
>>
>> True, since Ruby has only an NFA (rather than a DFA) regex engine. I
>> find this equivalent code a little bit more readable, though:
>>
>> /^(?:s(?:oo?m|a[vf])e|catch|blarg|wrest|template)/i
>
> Probably. But readable != efficient. There's definitively more
> backtracking in this RX than in the other one.
Where? I don't see it. If anything, it looks like there is less
potential backtracking, since the e after the first alternation is not
duplicated.