0

I am wondering if there is a 'smart' way (one regex expression) to extract IDs from the following paragraph:

...
imgList = '9/optimized/1260089_fpx.tif,0/optimized/1260090_fpx.tif';
...

The result shoul be a list containing 1260089 and 1260090. The count of the IDs might be up to 10.

I need something like:

re.findall('imgList = (some expression)', string)

Any ideas?

4 Answers 4

1

Best would be to use a single regex finding all the numbers. I call for re.findall

>>> imgList = '9/optimized/1260089_fpx.tif,0/optimized/1260090_fpx.tif'
>>> import re
>>> re.findall('optimized/([0-9]*)_fpx', imgList)
['1260089', '1260090']

You could of course make the regex stronger, but if the data is as you indicated, this should suffice.

Sign up to request clarification or add additional context in comments.

Comments

0
import re

s = '9/optimized/1260089_fpx.tif,0/optimized/1260090_fpx.tif'

print(re.findall(r'(\d+)_fpx.tif', s))

Comments

0

If the optimzed/ an _fpx part is not ensured and the ID is between 7 and 10 digits you could do something like

import re
re.findall('[\d]{7,10}', imgList)

This will find a 7 to 10 digit number in the string, hence, IDs with 0-6 or more than 10 digits will be excluded.

1 Comment

This approach is not what I was looking for, but might get the job done. Thanks!
0
import re
imgList = '9/optimized/1260089_fpx.tif,0/optimized/1260090_fpx.tif'
re.findall(r'([0-9]){7}',imgList)

['1260089', '1260090']

The code can only meet your situation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.