C# - Regex - How to replace a matched group in 1 line

In the example below, we want to replace all HTML span elements, with their inner content.

We create a C# regular expression to match the span element, and put parens around the part of the expression that matches the inner content. The parens create a matched group that we can reference in our replace-with string.

The $ syntax is used to refer to a matched group by number. In this case, we have only one group, so our replace-with string is $1.

Note that the regular expression will match two times, once for each span.

using System;
using System.Text.RegularExpressions;
 
namespace Zuga.net
{
    class Program
    {
        static void Main(string[] args)
        {
            var input = "one <span>two</span> three <span>four</span> five";
            var pattern = "<span>(.*?)</span>";
            var replacement = "$1";
 
            var output = Regex.Replace(input, pattern, replacement);
 
            Console.WriteLine("Input:  {0}", input);
            Console.WriteLine("Output: {0}", output);
        }
    }
}

The question mark makes the inner subexpression non-greedy.

Program output:

Input:  one <span>two</span> three <span>four</span> five
Output: one two three four five

Ads by Google


Ask a question, send a comment, or report a problem - click here to contact me.

© Richard McGrath