Say I have this string. "+2x-10+5"
. I want to split it into ['+2x','-10','+5']
.
So, I can use regular expression to make it ['2x','10','5']
, but I want to keep the separators, which are the plus and minus signs.
How do I do this?
Say I have this string. "+2x-10+5"
. I want to split it into ['+2x','-10','+5']
.
So, I can use regular expression to make it ['2x','10','5']
, but I want to keep the separators, which are the plus and minus signs.
How do I do this?
Don't use re.split()
, use re.findall()
with a regexp that matches each sub-expression.
import re
s = "+2x-10+5"
result = re.findall(r'[-+]\w+', s)
print(result)