Search code examples
.netregexwindowspython-2.7filepath

Python 2.7 Filepath Regex match Too many )'s


I am trying to match a specific word in between backslashes in the windows file path of a file. The value to match is "Psb+" for this case

X:\Guli\Proc(Loops)\Read\Psb+\İngil\Fate\5.0X[Lxss.] [Psb+] Instructions.txt

Due to software limitations I cannot use import os but I can use Pythonnet.

My regex is like this

pFilePath = "X:\Guli\Proc\(Loops)\Read\Psb+\İngil\Fate\5.0X\[Lxss.] [Psb+] Instructions.txt"
searchtext = "Psb+"    
pm = re.search(r'(?i)(?<="\\")' + searchtext + r'(?="\\")', pFilePath)

It runs OK in the Visual Studio Core but when I try it on my environment, I get this error:

parsing ............. - Too many )'s.

I am stuck with this. Is there a more clever way of doing this?


Solution

  • After trying a lot of things,

    I got around the issue by implementing Directory.GetDirectories Method using IronPython.

    import clr
    from System.IO import Path, File, Directory
    

    https://learn.microsoft.com/en-us/dotnet/api/system.io.directory.getdirectories?view=netcore-3.1

    Then I found a way to import os by using symbolic links.