jftuga jftuga - 1 year ago 186
Python Question

Python 3.5 regular expression matching for directories

I am using the Python 3.5

module with this code:

>>> test
'\\\\\\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

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

How can I accomplish this operation?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download