Click here to Skip to main content
15,911,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
extract values from a string between delimiters (/) but keep (\/)

string = "a/b\/c/d"

Expected output
['a', 'b\\/c', 'd']

What I have tried:

string = "a/b\/c/d"
string.split('/')
['a', 'b\\', 'c', 'd']
Posted
Updated 23-Mar-17 2:04am

1 solution

You can use a regular expression split:
Python
import re

string = "a/b\/c/d"
print string.split('/')

pattern = "(?<!\\\\)/"
print re.split(pattern, string)

Output:
['a', 'b\\', 'c', 'd']
['a', 'b\\/c', 'd']

(?<!\\\\)/: Matches if the current position in the string is not preceded by a match for the back slash.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900