Regex split string but not at the beginning

I need to split strings like this:
but exclude the first ‘-‘


captures all the ‘-‘ and ‘+’ but sadly gets the first one too:

s = '-89p-98u+2s-26y+97q+67r+71w-52t-3735+80z-7x+17v'
re.split(r'[-|+]', s)
['', '89p', '98u', '2s', '26y', '97q', '67r', '71w', '52t', '3735', '80z', '7x', '17v']

How do i exclude the first ‘-‘?

>Solution :

You could use a negative lookbehind to prevent splitting just after the start of the line:

>>> s = '-89p-98u+2s-26y+97q+67r+71w-52t-3735+80z-7x+17v'
>>> re.split(r'(?<!^)[-|+]', s)
['-89p', '98u', '2s', '26y', '97q', '67r', '71w', '52t', '3735', '80z', '7x', '17v']

Leave a Reply