1

I have to explode the following string based on "\". The probelm is, string.split('\') does not work and the re.split("\/",text). I have no idea.

the string is simple

D123D\user.name

the first part is sometimes longer.

2 Answers 2

6

You need to escape the backslash in a Python string literal:

string.split('\\')

Demo:

>>> r'D123D\user.name'.split('\\')
['D123D', 'user.name']
Sign up to request clarification or add additional context in comments.

Comments

1
re.split(r"\",text)

should work as well

1 Comment

Raw strings with trailing odd number of backslashes do not work. See Why can’t raw strings (r-strings) end with a backslash?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.