Reputation: 73
I have an if statement in a script to see if a directory given via argparse is a UNC path. But does not end with a trailing slash.
import re
#dest is set by argparse. but for this purpose i'll set it manually.
#originally i was escaping each \ in dest. But I found that argparse left them alone.
dest = '\\server-name\folder\subfolder'
if re.match ('^\\+[\w\W]+', dest ) and not re.match( '^\\+[\w\W]+\\$', dest):
dest = dest + '\\'
I've been playing with this in ipython. The first statement is failing to match. I used the RxToolkit in Komodo IDE, it shows the regex as matching. I tried this webtool: http://www.pythonregex.com/ it matches as well. Any ideas?
Upvotes: 3
Views: 67
Reputation: 71538
You are passing this to the re:
^\+[\w\W]+
Because \\
means \
. What you need to do is to raw your regex strings, by using r
:
if re.match(r'^\\+[\w\W]+', dest ) and not re.match(r'^\\+[\w\W]+\\$', dest):
^ ^
Upvotes: 5
Reputation: 368964
The string "\\"
represent a single backslash \
.
>>> print('\\')
\
You need to escape \
or should use raw string literal to represent two backslashes.
>>> print('\\\\')
\\
>>> print(r'\\')
\\
BTW, re.match
check for match at the beginning of the string; you can omit the leading ^
.
Upvotes: 4