- s='99669999996669999996699666699666999966699666699'
复制代码 现在想匹配到- ['99', '66', '999999', '666', '999999', '66', '99', '6666', '99', '666', '9999', '666', '99', '6666', '99']
复制代码 也就是所有的连续字符。
这个是我的代码,感觉麻烦了- import re
- s='99669999996669999996699666699666999966699666699'
- p=re.compile(r'(.)(\1*)')
- l=p.findall(s) #l=[('9', '9'), ('6', '6'), ('9', '99999'), ('6', '66'), ('9', '99999'), ('6', '6'), ('9', '9'), ('6', '666'), ('9', '9'), ('6', '66'), ('9', '999'), ('6', '66'), ('9', '9'), ('6', '666'), ('9', '9')] 匹配了开头,跟接下来连续的字符
- li=[] #现在将每个开头与连续的部分合并
- for i in l:
- i=''.join(i)
- li.append(i)
- li #li就是结果
复制代码 抛砖引玉,应该有一个正则就搞定的方法 |