How to search for tags using Regular Expression?

3

I would like to create a regular expression that looks for tags:

<b>, <p>, <i>

How would you do this in regular expression?

    
asked by anonymous 16.02.2017 / 17:45

1 answer

2

You can use the expression @"\<([\/?\s?\w]+)\>

See the example below.

using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {

        foreach (Match match in Regex.Matches("<p> olá </ br>  nova linha </p>",  @"\<([\/?\s?\w]+)\>"))
        {
            Console.WriteLine("{0}", match.Value);
        }
    }
}

link

You can make Replace direct with Regex

using System;
using System.Text.RegularExpressions;

public class Program
{
    public static void Main()
    {
        string texto = "<p> olá </ br>  nova linha </p>";

        var match = Regex.Replace(texto,  @"\<([\/?\s?\w]+)\>", "");            

        Console.WriteLine(match);

    }
}

link

    
16.02.2017 / 17:58