I could go through iterating the file names, find the first number in a string, form a base string, counter + possible padding and end strings and try to match these. This, however, sounds a little too messy and makes me think there must be a better way. But is there?
I wanted to have simple solution, thus I have sacrificed memory and use several lists/maps to sort/group names. I packed strings into structs with extra fields for auxiliary information.
I'm using a special compare function. It isn't precisely const, since along with the comparing strings it also extracts and saves a prefix for every entry. (Actually I am comparing CVS-style file version and the prefix in my case is an id of a branch, sequences are adjacent versions of a file.)
It works like that. The input file list:
"filename_00001.png"
"filename_00002.png"
"filename_00003.png"
First strip extensions:
"filename_00001", "png"
"filename_00002", "png"
"filename_00003", "png"
Then during sorting extract from file name prefix: the name - all digits on the end. If prefixes are equal, then compare the numbers. (In my case input isn't padded with zeros):
"filename_00001", "png", "filename_"
"filename_00002", "png", "filename_"
"filename_00003", "png", "filename_"
After the sorting/prefix extraction, all sequences have the same prefix. If file doesn't have a number on the end, the whole file name is a prefix and it is by definition unique. You can also additionally save the extracted number:
"filename_00001", "png", "filename_", 1
"filename_00002", "png", "filename_", 2
"filename_00003", "png", "filename_", 3
Now they are all sorted and sorted properly, auxiliary information is out there prepared - all further operations are rather trivial. E.g. iterating over the list, one can extract all sequences with simple rule: prefix is the same/seq num is +1 compared to the prev entry.
IOW, it might be not much different from what you do. I am simply not afraid to throw a bit of memory/CPU on the operation since following them file operations are slower/more memory hungry by magnitude anyway.