Matching the parentheses of regular expressions with greedy

Source: Internet
Author: User

Today encountered a regular expression regex = ' <div class= ' div_result[\s\s]+?> ([\s\s]+) </div> '
I thought I would match it to the content of the Web page and get a complete string like <div class= "div_result...</div>, but the results only get the content between <div></div>, very puzzled , after the internet to find out, the original brackets have access to match the role:
(pattern) ——— match pattern and get this match.

In addition to representing 0 or one, you can suppress greedy matches by default, greedy matches (the more matches, the better), if there are multiple patterns at the same time greedy match, the final result is the result of competing compromises.

ImportRedefgetregresults (Reg, data): Pattern=Re.compile (reg) Resultlists=Re.findall (pattern, data)returnresultlistsif __name__=='__main__': S="abcd_123e FG hk456"Reg='abc.+ ([\s\s]+?) \d+'REG2='([\s\s]+?)'reg3='([\s]?)'REG4='([\s\s]+?)'Reg5='([\s]+)'    PrintGetregresults (Reg, s)PrintGetregresults (REG2, s)PrintGetregresults (REG5, s)PrintGetregresults (reg3, s)PrintGetregresults (REG4, s)

Reference

Http://www.cnblogs.com/yirlin/archive/2006/04/12/373222.html

Http://www.cnblogs.com/graphics/archive/2010/06/02/1749707.html

Matching the parentheses of regular expressions with greedy

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.