Bowie Bailey wrote:

[...]

An alternative solution would be this:

/style="[^>]+color:blue/

This looks better.  It is probably less resource-intensive than your
previous attempt and is definitely easier to read.  But why are you
looking for > when you anchor the beginning with a quote?

How about this:

    /style="[^"]+?color:blue/

This is also non-greedy, so it will start looking for the "color:blue"
match at the beginning of the string instead of having the + slurp up
everything up to the quote and then backtracking to find the match.

The regexp engine doesn't slurp. It just scans from left to right, noting "I might have to come back here" along the way.

For SA purposes, you may want to limit the search as well.

    /style="[^"]{1,20}?color:blue/

This way, it will stop looking after 20 characters.  This prevents it
from using lots of memory if the quotes aren't closed.

Good point.

But this will certainly involve some backtracking, especially if
there is even more text after the "color:blue" but before the
closing > character, for example the "font-size:small" text.

No it won't. It will scan once and quit. It never encountered any other alternatives that would require backtracking.

David
--
"It's overkill of course, but you can never have too much overkill."

Reply via email to