I have images with variable names, the only consistant feature/thing is that the image number is located at the end of the name, just before the extension. For example: “im1.png”, “Image 02.tif”, “My3rdImage_3.jpg” , “Whatever_17_MoreWhatever-31.tiff”.
What would be the best way to find the last number?
I can always do:
- Find the last point.
- Go back while I detect digits
- Return the string between the last detected digit (included) and the point (excluded). But is there a better/faster/automatic way to do it?
Advertisement
Answer
Don’t know if it is “the best way” but you could do this with regex and it will automatically pick the last digit in a string:
EDIT
This pattern searches for any digit (or digits) which isn’t followed by another digit in the string. So you always get the last number as a result.
With re.search
:
lst = ["im.png", "Image 02.tif", "My3rdImage_3.jpg" , "Whatever_17_MoreWhatever-31.tiff", "My4rdImage22_445.jpg"] pat = r"(d+)(?!.*d+)" for elem in lst: tmp = re.search(pat, elem) if tmp: print(tmp[0]) 02 3 31 445
Or (credit to @Mad Physicist) a pattern which searches for “A single group of digits followed by non-digits, then end-of-string”
lst = ["im.png", "Image 02.tif", "My3rdImage_3.jpg" , "Whatever_17_MoreWhatever-31.tiff", "My4rdImage22_445.jpg"] pat = r"(d+)(?=D*$)" for elem in lst: tmp = re.search(pat, elem) if tmp: print(tmp[0]) 02 3 31 445
re.findall
import re lst = ["im1.png", "Image 02.tif", "My3rdImage_3.jpg" , "Whatever_17_MoreWhatever-31.tiff"] pattern = r"d+" for elem in lst: tmp = re.findall(pattern, elem) print(tmp) print(tmp[-1], 'n') ['1'] 1 ['02'] 02 ['3', '3'] 3 ['17', '31'] 31