I’m splitting a string with some separator, but want the separator matches as well:
JavaScript
x
6
1
import re
2
3
s = "oren;moish30.4.200/-/v6.99.5/barbi"
4
print(re.split("d+.d+.d+", s))
5
print(re.findall("d+.d+.d+", s))
6
I can’t find an easy way to combine the 2 lists I get:
JavaScript
1
3
1
['oren;moish', '/-/v', '/barbi']
2
['30.4.200', '6.99.5']
3
Into the desired output:
JavaScript
1
2
1
['oren;moish', '30.4.200', '/-/v', '6.99.5', '/barbi']
2
Advertisement
Answer
From the re.split
docs:
If capturing parentheses are used in pattern, then the text of all groups in the pattern are also returned as part of the resulting list.
So just wrap your regex in a capturing group:
JavaScript
1
2
1
print(re.split(r"(d+.d+.d+)", s))
2