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!