4

I am trying to write a function in C# to replace all occurances of a regex pattern with a custom string. I need to use the match string to generate the replace string so I am trying to loop over the matches rather than use Regex.Replace(). When I debug my code, the regex pattern matches part of my html string and goes into the foreach loop, however, the string.Replace function doesn't replace the match. Does anyone know what is causing this to happen?

Simplified version of my function:-

public static string GetHTML() {
    string html = @"
        <h1>This is a Title</h1>
        @Html.Partial(""MyPartialView"")
    ";

    Regex ItemRegex = new Regex(@"@Html.Partial\(""[a-zA-Z]+""\)", RegexOptions.Compiled);
    foreach (Match ItemMatch in ItemRegex.Matches(html))
    {
        html.Replace(ItemMatch.Value, "<h2>My Partial View</h2>");
    }

    return html;
}
2
  • string objects are immutable, to further explain @sethflowers answer. Commented Sep 27, 2012 at 15:34
  • 1
    Why are you using the Compiled option? You should use that only when you have a demonstrated need for it. The performance boost it offers is not that great, and it does not come for free. ref Commented Sep 27, 2012 at 19:31

5 Answers 5

7

string.Replace returns a string value. You need to assign this to your html variable. Note, also that it replaces all occurrences of the matched value, meaning you likely don't need your loop.

html = html.Replace(ItemMatch.Value, "<h2>My Partial View</h2>");

Returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, added the answer myself when I realised but you beat me to it.
1

You are not reallocating to html

so:

html = html.Replace(ItemMatch.Value, "<h2>My Partial View</h2>"); 

Comments

0

As the other answers state, you are not assigning the resulting value.

I would add, that your foreach cycle doesn't make much sense and you could go with inline replace:

Regex ItemRegex = new Regex(@"@Html.Partial\(""[a-zA-Z]+""\)", RegexOptions.Compiled);
html = ItemRegex.Replace(html, "<h2>My Partial View</h2>");

Comments

0

How about this? That way you are using the value from the match to replace with?

The biggest issue however was that you weren't reassigning the result of the replace to the html variable.

using System;
using System.Text.RegularExpressions;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            var html = @"
                            <h1>This is a Title</h1>
                            @Html.Partial(""MyPartialView"")
                        ";

            var itemRegex = new Regex(@"@Html.Partial\(""([a-zA-Z]+)""\)", RegexOptions.Compiled);
            html = itemRegex.Replace(html, "<h2>$1</h2>");

            Console.WriteLine(html);
            Console.ReadKey();
        }
    }
}

Comments

-1

Feeling very silly. The string is ummutable so I need to recreate it.

html = html.Replace(ItemMatch.Value, "<h2>My Partial View</h2>");

1 Comment

You might notice the small time intervals between posting. I was writing my anwser while the other were posted so I didn't see them. I'm sorry I didn't accept your answer I perhaps did not ask my question very well.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.