Use Python regular expressions to intercept pair brackets

Source: Internet
Author: User
This article describes how to intercept parentheses in Python regular expressions, and involves Python regular expression matching techniques, for more information about how to use Python regular expressions to intercept parentheses, see the following example. We will share this with you for your reference. The details are as follows:

Strs = '1 (2 (3 (4 (5 (67) 6) 7) 8) 9) 0' reg1 = re. compile ('([()]) comment') # A pair of parentheses reg2 = re. compile ('([()] | \ ([()] Bytes) * \)') # two pairs of parentheses reg3 = re. compile ('([()] | \ ([()] bytes | ([()] | \ ([()] Bytes )*\))*\)') # layer 3

Function

# Matching the regular expression def getReg (self, count, bracket = '()') in pairs '()'): leftBracket = bracket [0] rightBracket = bracket [1] count-= 1 regBasic = leftBracket + '(? : [^ '+ LeftBracket + rightBracket +']) * '+ rightBracket if count <0: regBasic = ''if count> 0: for I in xrange (count ): tempNum = regBasic. rfind ('*')-1 regBasic = regBasic [: tempNum] + "|" + regBasic [tempNum:] return regBasic

I hope this article will help you with Python programming.

For more articles about how to use Python regular expressions to intercept pair brackets, refer to PHP!

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.