代码如下
import re
str='''
1
2
3
1
2
3
4
1
2
3
4
5
1
'''
pattern=re.compile(r'(^1\n)(^(?!1)\d.*\n)',flags=re.M)
match=pattern.findall(str)
print(match)
打印结果:[('1\n', '3\n'), ('1\n', '4\n'), ('1\n', '5\n'), ('1\n', '')]
想要打印出[('1\n', '2\n3\n'), ('1\n', '2\n3\n4\n'), ('1\n', '2\n3\n4\n5\n'), ('1\n')]
或者[('1\n', '2\n','3\n'), ('1\n', '2\n','3\n','4\n'), ('1\n', '2\n','3\n','4\n','5\n'), ('1\n')]
我应该怎么写正则表达式,
我写出的最接近的也是pattern=re.compile(r'(^1\n)((^(?!1)\d.*\n)*)',flags=re.M)
结果是[('1\n', '2\n3\n', '3\n'), ('1\n', '2\n3\n4\n', '4\n'), ('1\n', '2\n3\n5\n', '5\n'), ('1\n', '', '')]
请大神指导一下