in reply to Multiple Regex evaluations or one big one?

"Am I better running a..." has to be qualified with what your design goal is.

Regarding computational efficiency: Alternation is pretty efficient nowadays. But if there's a possibility of lots of backtracking, and that possibility can be avoided by breaking things up into smaller experssions, smaller expressions are the way to go.

If the goal is readability, smaller expressions are often the way to go, though to some degree that's mitigated with the /x option and judicious use of white space.

If the goal is easy debugging, I don't think there's much question that easier to digest chunks of regular expressions are going to be simpler to debug than a wall of regexp metamumbojumbo. (That even applies to people who have invested considerable time and effort in learning RE's inside and out as well as reading MRE, in my opinion.)


Dave

  • Comment on Re: Multiple Regex evaluations or one big one?

Replies are listed 'Best First'.
Re^2: Multiple Regex evaluations or one big one?
by JavaFan (Canon) on Jul 27, 2011 at 22:53 UTC
    If there's any backtracking, you have lost the first battle already.

    The ultimate win when optimizing regular expressions is to keep the regular expression engine idle. To quote some Perl core hacker (Yves? Jarkko? Nicholas?), "Perls regular expression engine isn't fast. It often *looks* fast, but that's because the optimizer does such a good job". Splitting the pattern up in smaller parts increases the chance the regular expression can just stay in bed - and that the optimizer does all the heavy lifting.