2014-04-22 53 views
1

有沒有更好的方式找到字符串中的數字開始'_v'代表版本號?我想要的只是'001'蟒蛇找到與領先'_v'的數字

filename = 'greatv02_v001_jam.mb' 

parts = re.split('_v|\_',filename) 
>>['greatv02', '001', 'jam.mb'] 

b = re.findall(r'\d+', filename) 
>>['02', '001'] 

有沒有一種方法可以沿着這些行分割字符串?

parts = re.split('_v###_',filename) 

parts = re.split('_v*_',filename) 

回答

1

你可以使用lookarounds

>>> filename = 'greatv02_v001_jam.mb' 
>>> import re 
>>> re.findall(r'(?<=_v)\d+', filename) 
['001'] 
>>> 

>>> filename = 'greatv02_v001_av456jam.mb' 
>>> re.findall(r'(?<=_v)\d+', filename) 
['001'] 

>>> filename = 'greatv02_v001_v456jam.mb' 
>>> re.findall(r'(?<=_v)\d+', filename) 
['001', '456'] 
>>> 
1

醜陋,但你可以partition文件名兩次

>>> filename.partition('_v')[2].partition('_')[0] 
'001' 
1

使用正則表達式的分組是這樣的:

.*_v(\d+).* 

演示:

>>> filename = 'greatv02_v001_jam.mb' 
>>> pattern = re.compile(r'.*_v(\d+).*') 
>>> re.search(pattern, filename).group(1) 
'001' 
1

如何在正則表達式_v(?P<version>\d+).*

>>> regex = re.compile("_v(?P<version>\d+).*") 
>>> r = regex.search(string) 

# List the groups found 
>>> r.groups() 
(u'001',) 

# List the named dictionary objects found 
>>> r.groupdict() 
{u'version': u'001'} 

# Run findall 
>>> regex.findall(string) 
[u'001'] 

# Run timeit test 
>>> setup = ur"import re; regex =re.compile("_v(?P<version>\d+).*");string="""greatv02_v00 ... 
>>> t = timeit.Timer('regex.search(string)',setup) 
>>> t.timeit(10000) 
0.005126953125