Berkyjay
Berkyjay

Reputation: 165

Extracting multiple segments from a string in Python

I'm working on gathering up a list of image sequences from multiple directories. I'm using a TCL expression within Nuke (compositing package) to generate a sting that lists the contents of each directory. The returned string looks like so:

Draft {Test_02_v002_Global_alp_%04d.png 1-12} {Test_02_v002_Global_col_%04d.png 1-12} Thumbs.db

The Draft and Thumbs.db are unwanted parts of the string (non-image files within the directory). What I want to get is this:

Test_02_v002_Global_alp_%04d.png 1-12 

Test_02_v002_Global_col_%04d.png 1-12

I am able to do this if there is only one image sequence in the directory using the following code:

start = "{"
end = "}"
re.search('%s(.*)%s' % (start, end), string).group(1) 

But I can't figure out how to get this to work when more than one image sequence exists.

Upvotes: 1

Views: 70

Answers (1)

user2555451
user2555451

Reputation:

You can use re.findall:

>>> from re import findall
>>> mystr = "Draft {Test_02_v002_Global_alp_%04d.png 1-12} {Test_02_v002_Global_col_%04d.png 1-12} Thumbs.db"
>>> findall("{(.*?)}", mystr)
['Test_02_v002_Global_alp_%04d.png 1-12', 'Test_02_v002_Global_col_%04d.png 1-12']
>>>

Upvotes: 2

Related Questions