python正则表达式提取字符串中的数字

2024-11-02 13:33:37
推荐回答(2个)
回答1:

import sys
import os
import re
if __name__ == '__main__':
SEARCH_PAT = re.compile(r'iops\s*=\s*(\d+)')
src_line = 'io=8192.0MB, bw=24407KB/s, iops=6101 , runt=343698msec'
pat_search = SEARCH_PAT.search(src_line)
if pat_search != None:
print pat_search.group(1)

回答2:

直接find就行了吧
a="io=8192.0MB, bw=24407KB/s, iops=6101 , runt=343698msec"
a[a.find('iops=')+5:a.find(',', a.find('iops'))]