C # Regular expression extracts the SRC address in an IMG tag in HTML

Source: Internet
Author: User

Baidu to the one, here directly posted

Http://blog.csdn.net/smeller/article/details/7108502#comments

In general, an HTML document has a lot of tags, such as "We can think of how to build this regular expression from the HTML tag format. The first thing to think about the IMG tags are several ways to write, ignoring the case, the following list of possible IMG tags can occur.

These tags do not need to be considered, because there is no picture resource address.

Some of these tags have a picture resource address, and another feature is a quotation mark pair, which may be single or double quotation marks. Because there is no need to match the quotes pair at the same time, the regular expression can be written like this: @ "]*) \s*/?\s*> "

Because there may be other parameters between IMG and SRC, so "]*?\bsrc\s*=\s*[" "']?\s*" (? [ ^\s "" ' <>]*) [^<>]*?/?\s*> "
/images/pic.jpg "/>
Problems such as the possibility of folding the line with a carriage return sometimes occur, so include a carriage return line and tab character where there are spaces, and no spaces, tabs, carriage returns, or newline characters 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 a static method that gets all the image addresses in the HTML.

    /// <summary>       ///gets the URL of all the pictures in the HTML. /// </summary>       /// <param name= "Shtmltext" >HTML code</param>       /// <returns>List of URLs for pictures</returns>        Public Static string[] Gethtmlimageurllist (stringShtmltext) {         //define a regular expression to match an IMG tagRegex regimg =NewRegex (@"]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*["'"? [\s\t\r\n]* (? [^\s\t\r\n "" ' <>]* ") [^<>]*?/? [\s\t\r\n]*>", regexoptions.ignorecase); //search for matching stringsMatchCollection matches =regimg.matches (Shtmltext); inti =0; string[] Surllist =New string[matches.                 Count]; //get a list of matches      foreach(Match matchinchmatches) Surllist[i+ +] = match. groups["Imgurl"].         Value; returnsurllist; }  

C # Regular expression extracts the SRC address in an IMG tag in HTML

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.