C # Regular expression matches the picture path in HTML

Source: Internet
Author: User

One of the most recent projects on Web page drawing is my own development, which is to use regular expressions to match the image tags.

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.

  1. <summary>
  2. /// Get the URL of all pictures in HTML.
  3. // </summary>
  4. /// <param name= "Shtmltext" >html code </param>
  5. /// <returns> URL List of images </returns>
  6. public static string[] Gethtmlimageurllist (string shtmltext)
  7. {
  8. //define regular expressions to match an IMG tag
  9. Regex regimg = 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);
  10. //Search for matching strings
  11. MatchCollection matches = regimg.matches (Shtmltext);
  12. int i = 0;
  13. string[] surllist = new string[matches.  Count];
  14. //Get a list of matches
  15. foreach (Match match in matches)
  16. surllist[i++] = match. groups["Imgurl"].  Value;
  17. return surllist;
  18. }

C # Regular expression matches the picture path 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.