tags:

views:

37

answers:

2

I've got the following code:

Regex.Replace(text, words.ToString(), "<dfn title=\"" + this.FindDefinition("$0")  + "\">$0</dfn>", RegexOptions.IgnoreCase | RegexOptions.Compiled);

The problem I'm having is with the FindDefinition method. I would like to send in the originale word to make a lookup and return the definition text. Is this possible or do I need to create a template like this:

Regex.Replace(text, words.ToString(), "<dfn title=\"{$0}\">$0</dfn>", RegexOptions.IgnoreCase | RegexOptions.Compiled);

And then do a search for it to replace it with my definition?

I've also tried with:

Regex.Replace(text, words.ToString(), this.ReplaceWord, RegexOptions.IgnoreCase | RegexOptions.Compiled); 

private string ReplaceWord(Match m)
{
    return "<dfn title=\"" + this.FindDefinition(m.Value) + "\">$0</dfn>";
}

Which works fine with the FindDefinition method, but then I have a problem getting the original value.

Anything I'm missing?

A: 

Try this:

return string.Format("<dfn title=\"{0}\">{1}</dfn>",
                     this.FindDefinition(m.Value),
                     m.Value);
Yannick M.
You're of course right.. I must have done something wrong when I tested this earlier. Thanks :).
Frederik Vig
A: 

You're building XML by hand, it's almost always plain wrong.

You'll need to use the evaluator (replaceword). Why don't you do something like:

private string ReplaceWord(Match m)
{
    return "<dfn title=\"" + this.FindDefinition(m.Value) + "\">"+m.Value+"</dfn>";
}

I'd tend to use something like m.Groups[0].Value and use grouping in regex.

Pasi Savolainen