0

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

2
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by marcinkuptel
0

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']
"""
1

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:

Votes + Comments
nice!
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.