in reply to Re: Re: stripping
in thread stripping

If he knows that the only other parentheses he is going to have in his data are not going to have quotes before or after them, is it more efficient to try to match less? Or is that not an issue?

Queue
has much to learn

Replies are listed 'Best First'.
Re: Re: Re: Re: stripping
by sauoq (Abbot) on Jan 05, 2003 at 23:11 UTC
    If he knows that the only other parentheses he is going to have in his data are not going to have quotes before or after them, is it more efficient to try to match less? Or is that not an issue?

    Generally, it's not an issue. In fact, a regular expression that matches more might be more efficient in some circumstances¹. The only thing that can really cripple your efficiency is an expression which causes the regular expression engine to do a whole lot of backtracking.

    For the most part, you really don't have to worry about regular expression efficiency at all but you will, on occasion, run into a situation where an understanding of it is invaluable. When you do have to worry about it, the way to approach it isn't to construct an expression which will match less but to construct an expression which will allow the engine to work less in determining whether a match exists or not.

    1. E.g. You have to find all lines in a large file that contain the word "bar" but you also happen to know that all of the lines that contain the word "bar" will also start with "foo". You could use /^foo.*bar/ instead of /bar/ and, even though the expression matches more, you will quickly eliminate many lines. This is a contrived example but similar situations are not uncommon in real programming.

    -sauoq
    "My two cents aren't worth a dime.";