As far as a better way there is no such thing as long as your code solves the problem you set out to solve, and in this case it does. As far as performance goes there are probably faster ways to achieve the same thing but even with a list of thousands of string to parse the difference in speed would be minimal or indistinguishable. Therefore I assume you are instead asking for a different way to accomplish the same goal, so I have written a small function and several test segments. The function gives slightly more control.
def get_version(file_name, accuracy=0, sep=None):
'''
:string file_name is the string you want to parse
:int accuracy is the length of the output format
1 = 1 of 1.23.3
2 = 1.2 of 1.23.3
3 = 1.23 of 1.23.3
0 = 1.23.3 or full length
:string sep, is the string seperator you wish to use, 1.1, 1_1, 11 etc...
'''
if not sep:
sep = ''
data = file_name.split('.')
str_ver = data[0:-2]
ver = []
for i in str_ver:
if len(ver) < accuracy or accuracy == 0:
try:
if len(i)>1:
n = ''
for x in i:
try:
n+=str(int(x))
except:
pass
else:
n = str(int(i))
ver.append(n)
except:
pass
return sep.join(ver)
print get_version("httpd-2.2.31.tar.gz", 1, '--')
print get_version("httpd-2.2.31.tar.gz", 2, '::')
print get_version("httpd-2.2.31.tar.gz", 3, '_')
print get_version("httpd-2.2.31.tar.gz", 2)#what you were asking for
print get_version("httpd-2.2.31.tar.gz")