How to search for tags using Regular Expression?

Asked

Viewed 322 times

3

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

<b>, <p>, <i>

How would you do that in regular expression?

  • Maybe you can help!

  • You want to search for what? take what’s between them? erase?

  • actually delete @Juniornunes

  • delete everything inside the tag or just the tags?

  • Search in a text for example the user typed <i>hello<i/> I want to locate and remove tags.

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);
        }
    }
}

https://dotnetfiddle.net/2flyzC

You can do the Replace straight with the 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);

    }
}

https://dotnetfiddle.net/NO2Jc4

Browser other questions tagged

You are not signed in. Login or sign up in order to post.