Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python's regex Get folder name between name=' and /'

I want to extract all the folder names between "name=’" and "/’" under a list of strings using regex

string = "FileInfo(path='dbfs:/mnt/34334324/folder1/', name='folder1/', size=0), FileInfo(path='dbfs:/mnt/34334324/folder2/', name='folder2/', size=0),"

expected result = [folder1, folder2]

This is running in databricks so things like

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

from glob import glob
glob("/mnt/targetfiles/*/", recursive = True)

is not working

>Solution :

Is there always a "/" in the name? Yes

What happens if there are several? there are not several

Can the names contain escaped characters? no

Thus a simple regex would work:

import re
re.findall("(?<= name=').*?(?=/')", string)

output: ['folder1', 'folder2']

How it works:

(?<=name=')  # must be preceded by " name='"
.*?          # get the shortest string
(?=/')       # must be followed by "/'"

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading