<p>I have a regex call that I need help with.</p>

<p>I haven't posted my regex, because it is not relevant here.
What I want to be able to do is, during the Replace, I also want to modify the ${test} portion by doing a Html.Encode on the entire text that is effecting the regex.</p>

<p>Basically, wrap the entire text that is within the range of the regex with the bold tag, but also Html.Encode the text inbetween the bold tag.</p>

<pre><code>RegexOptions regexOptions = RegexOptions.Compiled | RegexOptions.IgnoreCase;
text = Regex.Replace(text, regexBold, @"&lt;b&gt;${text}&lt;/b&gt;", regexOptions);
</code></pre>

[{'answer_id': 46722, 'author': 'Ben Scheirman', 'author_id': 3381, 'author_profile': 'https://Stackoverflow.com/users/3381', 'pm_score': 2, 'selected': False, 'text': "<p>Don't use Regex.Replace in this case...  use..</p>\n\n<pre><code>foreach(Match in Regex.Matches(...))\n{\n    //do your stuff here\n}\n</code></pre>\n"}, {'answer_id': 46725, 'author': 'DevelopingChris', 'author_id': 1220, 'author_profile': 'https://Stackoverflow.com/users/1220', 'pm_score': 0, 'selected': False, 'text': '<p>If you do a Regex.Match,   the resulting match objects group at the 0th index, is the subset of the intput that matched the regex.</p>\n\n<p>you can use this to stitch in the bold tags and encode it there.</p>\n'}, {'answer_id': 46761, 'author': 'public static', 'author_id': 1368, 'author_profile': 'https://Stackoverflow.com/users/1368', 'pm_score': 0, 'selected': False, 'text': "<p>Can you fill in the code inside {} to add the bold tag, and encode the text?\nI'm confused as to how to apply the changes to the entire text block AND replace the section in the text variable at the end.</p>\n"}, {'answer_id': 46785, 'author': 'DevelopingChris', 'author_id': 1220, 'author_profile': 'https://Stackoverflow.com/users/1220', 'pm_score': 3, 'selected': True, 'text': '<p>Heres an implementation of this I\'ve used to pick out special replace strings from content and localize them.</p>\n\n<pre><code>        protected string FindAndTranslateIn(string content)\n        {\n            return Regex.Replace(content, @"\\{\\^(.+?);(.+?)?}", new MatchEvaluator(TranslateHandler), RegexOptions.IgnoreCase);\n        }\n\npublic string TranslateHandler(Match m)\n{\n    if (m.Success)\n    {\n        string key = m.Groups[1].Value;\n        key = FindAndTranslateIn(key);\n        string def = string.Empty;\n        if (m.Groups.Count &gt; 2)\n        {\n            def = m.Groups[2].Value;\n            if(def.Length &gt; 1)\n            {\n                def = FindAndTranslateIn(def);\n            }\n        }\n\n        if (group == null)\n        {\n            return Translate(key, def);\n        }\n        else\n        {\n            return Translate(key, group, def);\n        }\n    }\n    return string.Empty;\n}\n</code></pre>\n\n<p>From the match evaluator delegate you return everything you want replaced, so where I have returns you would have bold tags and an encode call, mine also supports recursion, so a little over complicated for your needs, but you can just pare down the example for your needs.</p>\n\n<p>This is equivalent to doing an iteration over the collection of matches and doing parts of the replace methods job. It just saves you some code, and you get to use a fancy shmancy delegate.</p>\n'}, {'answer_id': 46810, 'author': 'viggity', 'author_id': 4572, 'author_profile': 'https://Stackoverflow.com/users/4572', 'pm_score': 2, 'selected': False, 'text': '<p>There is an incredibly easy way of doing this (in .net). Its called a MatchEvaluator and it lets you do all sorts of cool find and replace. Essentially you just feed the Regex.Replace method the method name of a method that returns a string and takes in a Match object as its only parameter. Do whatever makes sense for your particular match (html encode) and the string you return will replace the entire text of the match in the input string. </p>\n\n<p>Example: Lets say you wanted to find all the places where there are two numbers being added (in text) and you want to replace the expression with the actual number. You can\'t do that with a strict regex approach, but you can when you throw in a MatchEvaluator it becomes easy.</p>\n\n<pre><code>public void Stuff()\n{\n    string pattern = @"(?&lt;firstNumber&gt;\\d+)\\s*(?&lt;operator&gt;[*+-/])\\s*(?&lt;secondNumber&gt;\\d+)";\n    string input = "something something 123 + 456 blah blah 100 - 55";\n    string output = Regex.Replace(input, pattern, MatchMath);\n    //output will be "something something 579 blah blah 45"\n}\n\nprivate static string MatchMath(Match match)\n{\n    try\n    {\n        double first = double.Parse(match.Groups["firstNumber"].Value);\n        double second = double.Parse(match.Groups["secondNumber"].Value);\n        switch (match.Groups["operator"].Value)\n        {\n            case "*":\n                return (first * second).ToString();\n            case "+":\n                return (first + second).ToString();\n            case "-":\n                return (first - second).ToString();\n            case "/":\n                return (first / second).ToString();\n        }\n    }\n    catch { }\n    return "NaN"; \n}\n</code></pre>\n\n<p>Find out more at <a href="http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.matchevaluator.aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.matchevaluator.aspx</a></p>\n'}]
['https://Stackoverflow.com/questions/46719', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1368/']