用正则表达式分析正则表达式!求正则表达式组数~

import re
r=r'(?<=charset=)(.+[[])(?P<groupname1>.*?)[s(bc)k]\("'  #待分析的正则表达式
s=re.subn(r'\[(?![?]).*?\]','',r)  #排除[]
s=re.subn(r'\\\(','',s[0])  #排除\(
s=re.subn(r'\?P<','',s[0])  #排除(?P<
l=re.findall(r'\((?!\?).*?\)',s[0])  #查找所有组
print len(l)  #打印组数

 

该代码已经应用于我的开源项目http://pyretoolkit.appspot.com/

原文地址:https://www.cnblogs.com/booolee/p/1561339.html