Remove HTML tags from webpages using regular expressions

Source: Internet
Author: User

Remove HTML tags from webpages using regular expressions
Regular Expression html
After capturing the data of a webpage (such as the description), if it is displayed as is, the format may be messy because it contains no closed HTML Tag, it may also contain HTML tags that are confusing and disrupt the reservation format. if you delete all the HTML tags in it, it may cause reading difficulties (such as a and IMG tags). It is best to delete a part and keep a part.

In a regular expression, it is easy to understand how to determine whether to include certain strings, but how to determine whether to include certain strings (a string, not a character, or a certain character) it is indeed a confusing thing.

<(?! ((/? \ S? Li) | (/? \ S? Ul) | (/? \ S? A) | (/? \ S? IMG) | (/? \ S? BR) | (/? \ S? SPAN) | (/? \ S? B) [^>] +>

This regular expression is used to determine that the HTML tag does not contain Li/ul/A/img/BR/SPAN/B. In terms of the preceding requirements, the HTML Tag listed here should be deleted, this is what I found out after a long time.

(?! Exp) the position behind the matching is not the exp position.

/? \ S? At the beginning, I tried to write it to the front <, but the test failed.

The following is a simple function that concatenates tags to be retained, generates a regular expression, and then deletes unnecessary tags...

C # code
Private Static string removespecifyhtml (string CTX ){

String [] holdtags = {"A", "IMG", "Br", "strong", "B", "span"}; // tag to be retained

// <(?! ((/? \ S? Li) | (/? \ S? Ul) | (/? \ S? A) | (/? \ S? IMG) | (/? \ S? BR) | (/? \ S? SPAN) | (/? \ S? B) [^>] +>

String regstr = string. Format (@ "<(?! ((/? \ S? {0}) [^>] +> ", String. Join (@") | (/? \ S? ", Holdtags ));

RegEx Reg = new RegEx (regstr, regexoptions. Compiled | regexoptions. multiline | regexoptions. ignorecase );

Return Reg. Replace (CTX ,"");

}

Private Static string removespecifyhtml (string CTX ){

String [] holdtags = {"A", "IMG", "Br", "strong", "B", "span"}; // tag to be retained

// <(?! ((/? \ S? Li) | (/? \ S? Ul) | (/? \ S? A) | (/? \ S? IMG) | (/? \ S? BR) | (/? \ S? SPAN) | (/? \ S? B) [^>] +>

String regstr = string. Format (@ "<(?! ((/? \ S? {0}) [^>] +> ", String. Join (@") | (/? \ S? ", Holdtags ));

RegEx Reg = new RegEx (regstr, regexoptions. Compiled | regexoptions. multiline | regexoptions. ignorecase );

Return Reg. Replace (CTX ,"");

}
 

Fixed:

If Li is retained for the above regular expression, the link is also retained during actual operation. If a is retained, the ADDR is also retained. The solution is to add the \ B asserted.

<(?! ((/? \ S? Li \ B) | (/? \ S? Ul) | (/? \ S? A \ B) | (/? \ S? IMG \ B) | (/? \ S? Br \ B) | (/? \ S? SPAN \ B) | (/? \ S? B \ B) [^>] +>

C # code
Private Static string removespecifyhtml (string CTX ){

String [] holdtags = {"A", "IMG", "Br", "strong", "B", "span", "Li"}; // reserved tag

// <(?! ((/? \ S? Li \ B) | (/? \ S? Ul \ B) | (/? \ S? A \ B) | (/? \ S? IMG \ B) | (/? \ S? Br \ B) | (/? \ S? SPAN \ B) | (/? \ S? B \ B) [^>] +>

String regstr = string. Format (@ "<(?! ((/? \ S? {0}) [^>] +> ", String. Join (@" \ B) | (/? \ S? ", Holdtags ));

RegEx Reg = new RegEx (regstr, regexoptions. Compiled | regexoptions. multiline | regexoptions. ignorecase );

Return Reg. Replace (CTX ,"");

}

Remove HTML tags from webpages using regular expressions
Regular Expression html
After capturing the data of a webpage (such as the description), if it is displayed as is, the format may be messy because it contains no closed HTML Tag, it may also contain HTML tags that are confusing and disrupt the reservation format. if you delete all the HTML tags in it, it may cause reading difficulties (such as a and IMG tags). It is best to delete a part and keep a part.

In a regular expression, it is easy to understand how to determine whether to include certain strings, but how to determine whether to include certain strings (a string, not a character, or a certain character) it is indeed a confusing thing.

<(?! ((/? \ S? Li) | (/? \ S? Ul) | (/? \ S? A) | (/? \ S? IMG) | (/? \ S? BR) | (/? \ S? SPAN) | (/? \ S? B) [^>] +>

This regular expression is used to determine that the HTML tag does not contain Li/ul/A/img/BR/SPAN/B. In terms of the preceding requirements, the HTML Tag listed here should be deleted, this is what I found out after a long time.

(?! Exp) the position behind the matching is not the exp position.

/? \ S? At the beginning, I tried to write it to the front <, but the test failed.

The following is a simple function that concatenates tags to be retained, generates a regular expression, and then deletes unnecessary tags...

C # code
Private Static string removespecifyhtml (string CTX ){

String [] holdtags = {"A", "IMG", "Br", "strong", "B", "span"}; // tag to be retained

// <(?! ((/? \ S? Li) | (/? \ S? Ul) | (/? \ S? A) | (/? \ S? IMG) | (/? \ S? BR) | (/? \ S? SPAN) | (/? \ S? B) [^>] +>

String regstr = string. Format (@ "<(?! ((/? \ S? {0}) [^>] +> ", String. Join (@") | (/? \ S? ", Holdtags ));

RegEx Reg = new RegEx (regstr, regexoptions. Compiled | regexoptions. multiline | regexoptions. ignorecase );

Return Reg. Replace (CTX ,"");

}

Private Static string removespecifyhtml (string CTX ){

String [] holdtags = {"A", "IMG", "Br", "strong", "B", "span"}; // tag to be retained

// <(?! ((/? \ S? Li) | (/? \ S? Ul) | (/? \ S? A) | (/? \ S? IMG) | (/? \ S? BR) | (/? \ S? SPAN) | (/? \ S? B) [^>] +>

String regstr = string. Format (@ "<(?! ((/? \ S? {0}) [^>] +> ", String. Join (@") | (/? \ S? ", Holdtags ));

RegEx Reg = new RegEx (regstr, regexoptions. Compiled | regexoptions. multiline | regexoptions. ignorecase );

Return Reg. Replace (CTX ,"");

}
 

Fixed:

If Li is retained for the above regular expression, the link is also retained during actual operation. If a is retained, the ADDR is also retained. The solution is to add the \ B asserted.

<(?! ((/? \ S? Li \ B) | (/? \ S? Ul) | (/? \ S? A \ B) | (/? \ S? IMG \ B) | (/? \ S? Br \ B) | (/? \ S? SPAN \ B) | (/? \ S? B \ B) [^>] +>

C # code
Private Static string removespecifyhtml (string CTX ){

String [] holdtags = {"A", "IMG", "Br", "strong", "B", "span", "Li"}; // reserved tag

// <(?! ((/? \ S? Li \ B) | (/? \ S? Ul \ B) | (/? \ S? A \ B) | (/? \ S? IMG \ B) | (/? \ S? Br \ B) | (/? \ S? SPAN \ B) | (/? \ S? B \ B) [^>] +>

String regstr = string. Format (@ "<(?! ((/? \ S? {0}) [^>] +> ", String. Join (@" \ B) | (/? \ S? ", Holdtags ));

RegEx Reg = new RegEx (regstr, regexoptions. Compiled | regexoptions. multiline | regexoptions. ignorecase );

Return Reg. Replace (CTX ,"");

}

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.