Office_Employee_19981128_Temp32
string = 'Office_Employee_19981128_Temp32 '
new_string = re.sub('^Office_Employee_','',string)
new_string_2 = re.sub('\_Temp32$','',new_string)
print new_string_2
19981128
答案 0 :(得分:2)
使用re.search()
函数提取所需的数字:
string = 'Office_Employee_19981128_Temp32 '
matches = re.search(r'(?<=_)\d+(?=_)', string)
result = matches.group(0) if matches else ''
print result
输出:
19981128
编辑模式_(\d+)_
可用作替代
matches = re.search(r'_(\d+)_', 'Office_Employee_19981128_Temp32 ')
result = matches.group(1) if matches else ''
print result # will give same result as first approach
执行时间测量:
python3 -m timeit -n 1000 -s "import re;matches = re.search(r'(?<=_)\d+(?=_)', 'Office_Employee_19981128_Temp32 '); result = matches.group(0) if matches else ''"
1000 loops, best of 3: 0.0147 usec per loop
python3 -m timeit -n 1000 -s "import re;matches = re.search(r'_(\d+)_', 'Office_Employee_19981128_Temp32 '); result = matches.group(1) if matches else ''"
1000 loops, best of 3: 0.0148 usec per loop
python3 -m timeit -s "import re;matches = re.search(r'(?<=_)\d+(?=_)', 'Office_Employee_19981128_Temp32 '); result = matches.group(0) if matches else ''"
100000000 loops, best of 3: 0.00708 usec per loop
python3 -m timeit -s "import re;matches = re.search(r'_(\d+)_', 'Office_Employee_19981128_Temp32 '); result = matches.group(1) if matches else ''"
100000000 loops, best of 3: 0.00717 usec per loop