import re
pattern = re.compile(r"(\d{3})+$")
print pattern.match("123567").groups()
output result:
('567',)
I need the result is ('123','567')
.
The (\d{3})
only can output last group, but I want output every group.
import re
pattern = re.compile(r"(\d{3})+$")
print pattern.match("123567").groups()
output result:
('567',)
I need the result is ('123','567')
.
The (\d{3})
only can output last group, but I want output every group.
I am doing it in a bit of pythonic way
Solution 1
Python Code
p = re.compile(r'(?<=\d)(?=(?:\d{3})+$)')
test_str = "2890191245"
tmp = [x.start() for x in re.finditer(p, test_str)]
res = [test_str[0: tmp[0]]] + [(test_str[tmp[i]: tmp[i] + 3]) for i in range(len(tmp))]
Solution 2 (one liner)
print(re.sub("(?<=\d)(?=(\d{3})+$)", ",", test_str).split(","))