Use RegEx to find and replace specific HTML tags

I have a string containing dynamic HTML content.

I want to be able to find and replace all entries of certain HTML tags and replace them, but not the content inside them.

Specific HTML tags will be for the table - i.e. TABLE, TR and TD. Tags may contain attributes, otherwise they may be missing. How could one do this in C #?

Thanks in advance for your help!

0
source share
3 answers

Do not use regular expressions. Use the Html Agility Pack .

Look at this question , why not.

+4
source

This function may be sufficient:

public static string ReplaceTag(string input, string soughtTag, string replacementTag)
{
    return Regex.Replace(input, "(</?)" + soughtTag + @"((?:\s+.*?)?>)", "$1" + replacementTag + "$2");
}
+4
  e = "(< *?/*)div( +?|>)";
  repl = "\\1boo\\2"; 

, , , regex html.

+1
source

Source: https://habr.com/ru/post/1742466/


All Articles