C # The regular expression uses HTML to extract the image src from the webpage,

Source: Internet
Author: User
Tags open source cms

C # The regular expression uses HTML to extract the image src from the webpage,

Currently, the HoverTreeCMS project involves image processing. Based on the online case study, I wrote a method to obtain the image address in the content.

You can first look at the effect: http://tool.hovertree.com/a/zz/img/

Generally, an HTML document has many tags, such as
We can think about how to create this regular expression from the HTML tag format. First, I want to write several methods for img labels. If I ignore case sensitivity, I will list several possible causes of img labels.


You do not need to consider these labels because there is no image resource address.


These tags all have image resource addresses. In addition, they have pair quotation marks, which may be single or double quotation marks. Because quotation marks do not need to be matched at the same time, the regular expression can be written as follows: @ "] *) \ s */? \ S *>"


Because there may be other parameters between img and src, "] *? \ Bsrc \ s * = \ s * ["" ']? \ S *(? [^ \ s "" '<>] *) [^ <>] *? /? \ S *>"
/Images/pic.jpg "/>

For example, a carriage return or line break may occur. Therefore, a line break or TAB character must be included in a separate space, in addition, space, TAB, carriage return, and line feed characters cannot appear in the image address.

So the above regular expression can be changed to: @ "] *? \ Bsrc [\ s \ t \ r \ n] * = [\ s \ t \ r \ n] * ["']? [\ S \ t \ r \ n] * (? [^ \ s \ t \ r \ n "" '<>] *) [^ <>] *? /? [\ S \ t \ r \ n] *>"

The following is the class HvtHtmlImage used to obtain all image addresses in HTML:

1 using System. text. regularExpressions; 2 3 namespace HoverTree. hoverTreeFrame. hvtImage 4 {5 public class HvtHtmlImage 6 {7 // effect http://tool.hovertree.com/a/zz/img/ 8 /// <summary> 9 // get the URL of all images in HTML. 10 /// </summary> 11 /// <param name = "sHtmlText"> HTML code </param> 12 /// <returns> URL list of the image </returns> 13 public static string [] GetHvtImgUrls (string sHtmlText) 14 {15 // define a regular expression to match the img Tag 16 Regex m_hvtRegImg = new Regex (@ "] *? \ Bsrc [\ s \ t \ r \ n] * = [\ s \ t \ r \ n] * ["']? [\ S \ t \ r \ n] * (?  [^ \ s \ t \ r \ n "" '<>] *) [^ <>] *? /? [\ S \ t \ r \ n] *> ", RegexOptions. ignoreCase); 17 // reference: http://hovertree.com/hvtart/bjae/e4pya1x0.htm18 19 20 // search for matched string 21 MatchCollection matches = m_hvtRegImg.Matches (sHtmlText); 22 int m_ I = 0; 23 string [] sUrlList = new string [matches. count]; 24 25 // obtain the matching list 26 foreach (Match match in matches) 27 sUrlList [m_ I ++] = match. groups ["imgUrl"]. value; 28 return sUrlList; 29} 30} 31}

More: http://hovertree.com/hvtart/bjae/r42fdvjj.htm

ASP. NET Open Source CMS http://www.cnblogs.com/sosoft/p/cms.html

Development Technology Article collection http://www.cnblogs.com/sosoft/p/kaifajishu.html

Related Article

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.