tags:

views:

207

answers:

1

Background: I'm trying to generate a list of references from a page that has references throughout, and replace the reference markup with HTML.

I'm working in C#

Text looks something like:

The dog ate 3 cats and felt ill <ref name="something">http://cateater.com&lt;/ref&gt;

I'd like to use the Regex.Replace method to replace all the <ref>s, pushing them into a Hash that I can later render as a list of references.

Problem: it looks like the way to run code during the replace operation is to use a 'MatchEvaluator Delegate' that takes in a Match object, but doesn't seem like it can take other parameters.

The example on MSDN uses a class variable to accomplish this, but I'm hoping there is a more elegant solution.

+2  A: 

Since you are using XML tags, is there a reason the source document can't be processed as an XML DOM? Using XSLT would be a lot easier than a RegEx in that case.

If not then it is possible to effectively a paramatize the MatchEvaluator Delegate by using an anonymous function:-

MatchEvaluator evaluator = delegate(Match m)
{
    // code to return replacement
    // this code uses and modifies lookups
}

Dictionary<string, string> lookups = new Dictionary<string, string>();
sOutput = someRegex.Replace(sInput, evaluator);
AnthonyWJones
I wish! the source is a mix of HTML, wiki markup and other stuff like these <ref> elements.. :(
Kevin Davis