Is there a way to split the string:


on '^' character, but not if it follows a backslash? I have tried a regular expression like '[^\\]\^' but it removes also the 'o' in 'No'.



import re
pat = re.compile(r"((?:\\.|[^^])+)")
data = r"Autocolli\^sion:No^Pack\^age:10DB15"
""" my output --->
['Autocolli\\^sion:No', 'Pack\\^age:10DB15']

Thanks Gribouillis! Your solution works very well. I found something even simpler though. It's called the 'negative lookbehind assertion'.

print re.split(r"(?<!\\)\^", 'Autocolli\^sion:No^Pack\^age:10DB15')

The above line produces:

commented: nice! +4

Very nice. Note that there should be a difference if the data string begins (or ends) with ^.

Well spotted. You just saved me some debugging :)