Well... string parsing is always going to be be a bit of a crapshoot. Especially if you're trying to come up with something like parsing out the framenumbers, for a very general case, there will always be things that break it. In general, it's best if you can enforce some sort of uniformity on your filenames, so you can have a certain amount of assurance that it's going to work as intended.
However - to your specific regex - if you're looking specifically to catch cases that look like:
filename.####.ext
filename_####.ext.
...you're on the right track, but there's a few things I would fix.
First, some general advice - ALWAYS get in the habit of prefixing your regex pattern strings with "r", like: r'myPattern'
The 'r' marks it as a raw string, meaning backslashes aren't treated as escape sequences. Since python keeps the backslash for sequences it doesn't recognize, MOST of the time it won't make a difference, but it's best to just get in the habit now. I also like to always triple quote my regex patterns, for cases when I need to search for a quote mark...
Second - you probably want to make sure that your expression only matches at the END of the string - easiest way to do that is with a "$" character at the end. otherwise, if your file is:
f = "myawesome_filename_ver_1_frame.0001.exr"
...you'll get the wrong result. Also, I wouldn't limit your file extensions to only 3 characters - ".jpeg" and ".tiff" pop up pretty frequently. So at least allow 4 characters (and possibly just make it 1+). Finally, you'll probably want to start using groups - at minimum, around the framenumbers. So my final take would look something like:
pat = re.compile(r'''[.|_](\d+)\.(\w{3,4})$''')print pat.search(f).groups()
If you want to get fancy, python regular expressions allow you to name your groups, which I like. The main downside is that this is a python only extension, so if you try to copy your regex somewhere else, it won't work:
pat = re.compile(r'''[.|_](?P<frame>\d+)\.(?P<ext>\w{3,4})$''')
print pat.search(f).groupdict()