Skip to content Skip to sidebar Skip to footer

Python Re Library String Split But Keep The Delimiters/separators As Part Of The Next String

I've seen similar questions but none of them seem to help for what I'm trying to do and have spent ages trying to work thru the RE documentation with no luck so far. I am currentl

Solution 1:

If you are using python 3.7+ you can split by zero-length matches using re.split and positive lookahead:

string = 'a+0b-2a+b-b'
re.split(r'(?=[+-])', string)

# ['a', '+0b', '-2a', '+b', '-b']

Demo: https://regex101.com/r/AB6UBa/1

Solution 2:

Post a Comment for "Python Re Library String Split But Keep The Delimiters/separators As Part Of The Next String"