C # regular expressions match the image path in HTML,

Source: Internet
Author: User

C # regular expressions match the image path in HTML,

I have developed a function for retrieving web pages in my recent project, which is to use regular expressions to match image tags.

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 a static method for getting all the image addresses in HTML.

  1. /// <Summary>
  2. /// Obtain the URLs of all images in HTML.
  3. /// </Summary>
  4. /// <Param name = "sHtmlText"> HTML code </param>
  5. /// <Returns> List of image URLs </returns>
  6. Public static string [] GetHtmlImageUrlList (string sHtmlText)
  7. {
  8. // Define a regular expression to match the 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 matched strings
  11. MatchCollection matches = regImg. Matches (sHtmlText );
  12. Int I = 0;
  13. String [] sUrlList = new string [matches. Count];
  14. // Obtain the matching item list
  15. Foreach (Match match in matches)
  16. SUrlList [I ++] = match. Groups ["imgUrl"]. Value;
  17. Return sUrlList;
  18. }

 


& In C Language

& Can be used as the bitwise AND or address fetch Operator
The following describes two usage methods:
1. bitwise and operation bitwise AND operator "&" are binary operators. Its function is the binary phase corresponding to the two numbers involved in the operation. The result bit is 1 only when the two binary numbers are 1. Otherwise, the result bit is 0. The number of involved operations is supplemented.
For example, 9 & 5 can be written as follows: 00001001 (Binary complement of 9) & 00000101 (Binary complement of 5) 00000001 (Binary complement of 1) Visible 9 & 5 = 1.
Bitwise AND operations are usually used to clear some bits or retain some bits. For example, if a clears the high eight bits of 0 and retains the low eight bits, it can be used as a & 255 operation (255 of the binary number is 0000000011111111 ).
2. Get the address
& As The unary operator, the result is the address of the right operation object.
For example, & x returns the address of x.
The address itself is an abstract concept used to indicate the logical location of an object in the memory.

& In C Language

& Can be used as the bitwise AND or address fetch Operator
The following describes two usage methods:
1. bitwise and operation bitwise AND operator "&" are binary operators. Its function is the binary phase corresponding to the two numbers involved in the operation. The result bit is 1 only when the two binary numbers are 1. Otherwise, the result bit is 0. The number of involved operations is supplemented.
For example, 9 & 5 can be written as follows: 00001001 (Binary complement of 9) & 00000101 (Binary complement of 5) 00000001 (Binary complement of 1) Visible 9 & 5 = 1.
Bitwise AND operations are usually used to clear some bits or retain some bits. For example, if a clears the high eight bits of 0 and retains the low eight bits, it can be used as a & 255 operation (255 of the binary number is 0000000011111111 ).
2. Get the address
& As The unary operator, the result is the address of the right operation object.
For example, & x returns the address of x.
The address itself is an abstract concept used to indicate the logical location of an object in the memory.

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.