Take the 2-minute tour ×

I need to do something in regex but I'm really not good at it, long time didn't do that .


I need to change it to


Please trying to do it by using regular expression in Python.

share|improve this question
Why do you need to change this? Python can do just fine with / as a path separator. –  JoshD Oct 22 '10 at 3:34
add comment

5 Answers

I'm entirely in favor of helping user483144 distinguish "solution" from "regular expression", as the previous two answerers have already done. It occurs to me, moreover, that os.path.normpath() http://docs.python.org/library/os.path.html might be what he's really after.

share|improve this answer
add comment

why do you think you every solution to your problem needs regular expression??

>>> s="/a/c/a.doc"
>>> '\\'.join(s.split("/"))

By the way, if you are going to change path separators, you may just as well use os.path.join


mypath = os.path.join("C:\\","dir","dir1")

Python will choose the correct slash for you. Also, check out os.sep if you are interested.

share|improve this answer
it does not seem that the original poster believes that every solution needs regular expression. –  akonsu Oct 22 '10 at 2:13
@akonsu, almost all his previous posts are about regex. –  ghostdog74 Oct 22 '10 at 2:27
add comment

You can do it without regular expressions:

x = '/a/c/a.doc'
x = x.replace('/',r'\\')

But if you really want to use re:

x = re.sub('/', r'\\', x )
share|improve this answer
You can use a raw string to make this cleaner. –  Swiss Oct 22 '10 at 2:15
@Swiss Thanks for the suggestion - I fixed my answer. –  Graeme Perrow Oct 22 '10 at 3:32
add comment

\\ is means "\\" or r"\\" ?

re.sub(r'/', r'\\', 'a/b/c')

use r'....' alwayse when you use regular expression.

share|improve this answer
add comment
share|improve this answer
add comment

Your Answer


