I have a string in the following format;
s="part1,part2,part3,part4"
I can split the string into pieces by just invoking the s.split(",")
command.
Now, the question is what if I have a backslash escaped comma in the string? Assuming I have the following string,
s="part1,part2,pa\\,rt3,part4"
I'd like to be able to get ["part1","part2","pa,rt3","part4"]
as the result.
What I initially thought was to replace the \,
with a non-existent string, then split the string by using the split command and replace the non-existent string with a comma.
Can you think of a better way to deal with this problem?
Replacing it with a non-existing string is a nice option.
And otherwise, you could use a regular expression with a negative lookbehind like this:
re.split(r'(?<!\\),', 'part1,part2,pa\\,rt3,part4')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With