Posted in Python onJanuary 06, 2017
本文实例讲述了Python正则表达式实现截取成对括号的方法。分享给大家供大家参考,具体如下:
strs = '1(2(3(4(5(67)6)7)8)9)0' reg1 = re.compile('([()])∗') #一对括号 reg2 = re.compile('([()]|\([()]∗)*\)') #两对括号 reg3 = re.compile('([()]|\([()]∗|([()]|\([()]∗)*\))*\)') #三层
函数
#匹配成对括号正则表达式 def getReg(self, count, bracket = '()'): 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 + regBasic[tempNum:] return regBasic
Python正则表达式实现截取成对括号的方法
- Author -
Mr__fang声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@