I have a windows full path which I try to validate using regex. According to this
I have to add "\\\\\\\\"
but this is not working.
What am I doing wrong?
import re
regex1 = re.compile('downloads\\\\test_dir\\\\sql\\\\my-ee.sql')
s = "C:\x\xxx\temp\downloads\test_dir\sql\my-ee.sql"
gg = regex1.match(s)
gg
is None
.
So there are two things:
s
should be either with escaped back-slashes or as a raw string. I prefer the latter:s = r"C:\x\xxx\temp\downloads\test_dir\sql\my-ee.sql"
search
method instead of match
to be content with a partial match.Then, you can use \\\\
in the regular expression, or - as I prefer - a raw string again:
import re
regex1 = re.compile(r'downloads\\test_dir\\sql\\my-ee.sql')
s = r"C:\x\xxx\temp\downloads\test_dir\sql\my-ee.sql"
gg = regex1.search(s)
print(gg)