1

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

/a/c/a.doc

I need to change it to

\\a\\c\\a.doc

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

1
  • Why do you need to change this? Python can do just fine with / as a path separator. Commented Oct 22, 2010 at 3:34

5 Answers 5

5

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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

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

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

eg

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

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

2 Comments

it does not seem that the original poster believes that every solution needs regular expression.
@akonsu, almost all his previous posts are about regex.
1

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 )

1 Comment

You can use a raw string to make this cleaner.
0

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

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

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

Comments

0
'\\\'.join(r'/a/c/a.doc'.split("/"))

Comments

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.