Regular Expression summary, Regular Expression

Source: Internet
Author: User

Regular Expression summary, Regular Expression

1. Match tag a and its url:

Regex regA = new Regex(@"<a[\s]+[^<>]*href=(?:""|')([^<>""']+)(?:""|')[^<>]*>([^<>]+)</a>", RegexOptions.IgnoreCase);

Note: In the above regular expression,

Used to match various attributes before and after the href attribute:

[^<>]*

Used to match the url in the middle of the href attribute quotation marks:

([^<>""']+)

Used to match the content between tags:

([^<>]+)

 

2. Match the img tag and its url:

 Regex regImg = new Regex(@"]*src=(?:""|')([^<>""']+(?:\.jpg|\.jpeg|\.png|\.gif))(?:""|')[^<>]*>", RegexOptions.IgnoreCase);

3. matching the tag and the content in the tag center:

reg = new Regex(@"<dl class=""ksDl"">(?:(?!</dl>)[\s\S])*</dl>", RegexOptions.IgnoreCase);

Note: When the html string is as follows, it can be matched in two places,

<Dl class = "ksDl"> <div> test </div> </dl> <dl class = "ksDl"> <div> test </div> </dl>

If the regular expression is written like this:

reg = new Regex(@"<dl class=""ksDl"">[\s\S]*</dl>", RegexOptions.IgnoreCase);

Only one matching place is allowed. Pay attention to the functions of the following parts, which are excluded when matching the intermediate content </dl>

(?:(?!</dl>)[\s\S])*

 

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.