jftuga jftuga - 3 months ago 21
Python Question

Python 3.5 regular expression matching for directories

I am using the Python 3.5

re
module with this code:

>>> test
'\\\\192.168.1.2\\shared\\Department\\Travel\\FY 2015\\Travel Expense Statement Jul 25 2019.pdf'


I want to return
Department\Travel\FY 2015\Travel Expense Statement Jul 25 2019.pdf
. I have tried the following regex, but keep getting errors such as
sre_constants.error: nothing to repeat at position 12


x=re.compile( "shared\\[^\\](*?)" )
print( x.findall(test) )


or the empty result
['']
for:

x=re.compile( "shared\\\(.*?)" )


How can I accomplish this operation?

Answer

The problem with your regular expression is very simple, remove the ? character in your second regular expression. You just need .* that matches zero or more characters.

*? together means a lazy quantifier that matches as little as possible, so if you use .*?, it means "zero or more any characters, but as few as possible". As for the first regular expression, the * does not have a preceding atom to which it could apply to, hence the error.


In general case, you should rather use the ntpath module as in kennytm's answer there.