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