Hi,

Is there a way to split the string:

'Autocolli\^sion:No^Pack\^age:10DB15'

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

Marcin

hmm

import re
pat = re.compile(r"((?:\\.|[^^])+)")
data = r"Autocolli\^sion:No^Pack\^age:10DB15"
print(pat.findall(data))
""" 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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.