Using regular expressions to get rid of HTML code

Source: Internet
Author: User
Tags chr
Regular

A using system.text.regularexpressions;//requires a reference

Using regular expressions to remove content between "<" and ">"
private string Stripht (string strhtml)
{
Regex regex=new Regex ("<.+?>", regexoptions.ignorecase);
String Stroutput=regex. Replace (strHTML, "");
return stroutput;
}


Method Two (I wonder why this method occupies cpu100%)

public static string drophtml (String strhtml)
{
string [] Aryreg ={
@ "<script[^>]*?>.*?</script>",
@ "< (\/\s*)?!? ((\w+:) \w+) (\w+ (\s*=?\s* (["" "]) (\\[" "" tbnr]|[ ^\7] *?\7|\w+) |. {0}) | \s) *? (\/\s*)?> ",
@ "([\ r]) [\s]+",
@ "& (quot| #34);",
@ "& (amp| #38);",
@ "& (lt| #60);",
@ "& (gt| #62);",
@ "& (nbsp| #160);",
@ "& (iexcl| #161);",
@ "& (cent| #162);",
@ "& (pound| #163);",
@ "& (copy| #169);",
@ "&# (\d+);",
@ "-->",
@ "<!--. *"
};

string [] Aryrep = {
"",
"",
"",
"\"",
"&",
"<",
">",
" ",
"\xa1",//CHR (161),
"\xa2",//CHR (162),
"\xa3",//CHR (163),
"\xa9",//CHR (169),
"",
"\ r",
""
};

String Newreg =aryreg[0];
String stroutput=strhtml;
for (int i = 0;i<aryreg.length;i++)
{
Regex regex = new Regex (aryreg[i],regexoptions.ignorecase);
Stroutput = Regex. Replace (Stroutput,aryrep[i]);
}

Stroutput.replace ("<", "");
Stroutput.replace (">", "");
Stroutput.replace ("R", "");
return stroutput;

}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.