0

I would like to get, from the following string "/path/to/%directory_1%/%directory_2%.csv"

the following list: [directory_1, directory_2]. I would like to avoid using split by "%" my string. I was hoping to find a regex that could help me. However I cannot find the correct one.

For now, I have the following:

re.findall('%(.*)%', dirty_arg)

which output ["directory_1%/%directory_2"]

Do you have any recommandation about that?

Thank you very much for your help.

kofi_kari_kari
  • 420
  • 4
  • 18

1 Answers1

-1

Try this:

import re

regex = r"%(.*?)%"
dirty_arg = "/path/to/%directory_1%/%directory_2%.csv"

print(re.findall(regex, dirty_arg))

I've added ? to your regex which makes sure it matches as few times as possible. The output of this code is ['directory_1', 'directory_2']

Mark
  • 5,089
  • 2
  • 20
  • 31