[Mono-list] [PATCH] System.Text.RegularExpressions won't DTRT if you re-use patterns

Dan Lewis dihlewis@yahoo.co.uk
Thu, 1 Aug 2002 11:22:14 +0100 (BST)

Hi Juli!

Nice catch - I hadn't thought about that.

Your patch does indeed solve the problem. However the parser is now
being called for each new expression, whether it is in the cache or not.
Perhaps a better long term solution would be to cache a structure in the
lookup table.

At the moment, FactoryCache maps (pattern, options) => (factory). It
could be altered to map (pattern, options) => (factory, mapping,
group_count), so that the Regex constructor could pull these details out
too, and save both parse and compile time.


Juli Mallett wrote:

>Currently in the Regex code, there is factory caching code, however things
>like grouping need evaluated each time the Regex is constructed, the most
>simple/obvious solution is to move that code out of the section that runs
>only when there is no cached item in existence.  This happens when one
>constructs more than one Regex with the same pattern and the same options
>and e.g. tries to use $1 in Replace().  Example code which throws bogus
>exception would be something like:
>Foo = Regex.Replace(Foo, @"(.*)", "$1");
>// Here we get bogus exception
>Foo = Regex.Replace(Foo, @"(.*)", "$1");
>More or less.
>Here's a diff:

Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts