﻿

# 【问题解答】用Python的正则去匹配cpress值开始不等于零和重新全部等于零时的step time值

【问题】

python程序求助

 如图所示，现在想输出附件中cpress值开始不等于零和重新全部等于零时的“step time ”值，也就是想输出附件中“step time= 1.0001E-04 ”和“step time=2.500E-04” 这两个结果

【问题解答】

```#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Function:
【问题解答】用Python的正则去匹配cpress值开始不等于零和重新全部等于零时的step time值
http://www.crifan.com/python_re_findall_between_begin_not_zero_and_all_zero_step_time

Author:     Crifan Li
Version:    2013-06-20
Contact:    http://www.crifan.com/contact_me/
"""

import re;

def step_time_betwen_not_zero_and_zero():
#print "inputStr=",inputStr;

#matchedGroup = re.findall("step time= [\w\-]+\s+(?:node: \d+\s+cpress=0\.0+\s+)*node: \d+\s+cpress=(?:(?:[1-9]+\.\d+)|(?:0\.0*[1-9]+\d*))", inputStr, re.S);
#matchedGroup = re.search("step time= [\w\-]+\s+(node: \d+\s+cpress=0\.0+\s+)*node: \d+\s+cpress=[1-9]\d*\.\d+\s+", inputStr, re.S);
#matchedGroup = re.search("step\s+time=\s+[\w\.\-]+\s+(node: \d+\s+cpress=0\.0+\s+)*", inputStr, re.S);
#matchedGroup = re.findall("step\s+time=\s+[\w\.\-]+\s+(?:node: \d+\s+cpress=0\.0+\s+)*", inputStr, re.S);
#matchedGroup = re.findall("step\s+time=\s+[\w\.\-]+\s+(?:node: \d+\s+cpress=0\.0+\s+)*node:\s+\d+\s+cpress=[1-9]\d*\.\d+\s+", inputStr, re.S);
#matchedGroup = re.findall("step\s+time=\s+[\w\.\-]+\s+(?:node: \d+\s+cpress=0\.0+\s+)*(?:(?:node:\s+\d+\s+cpress=[1-9]\d*\.\d+\s+)|(?:frame=\d+\s+))+", inputStr, re.S);
#matchedGroup = re.findall("step\s+time=\s+[\w\.\-]+\s+(?:node: \d+\s+cpress=0\.0+\s+)*(?:(?:node:\s+\d+\s+cpress=[1-9]\d*\.\d+\s+)|(?:frame=\d+\s+)|(?:step\s+time=\s+[\w\.\-]+\s+))+", inputStr, re.S);
#matchedGroup = re.findall("step\s+time=\s+[\w\.\-]+\s+(?:node: \d+\s+cpress=0\.0+\s+)*(?:(?:node:\s+\d+\s+cpress=[1-9]\d*\.\d+\s+)|(?:frame=\d+\s+)|(?:step\s+time=\s+[\w\.\-]+\s+)|(?:node: \d+\s+cpress=0\.0+\s+))+", inputStr, re.S);
#matchedGroup = re.findall("step\s+time=\s+[\w\.\-]+\s+(?:node: \d+\s+cpress=0\.0+\s+)+(?:node:\s+\d+\s+cpress=[1-9]\d*\.\d+\s+)(?:(?:node:\s+\d+\s+cpress=[1-9]\d*\.\d+\s+)|(?:frame=\d+\s+)|(?:step\s+time=\s+[\w\.\-]+\s+)|(?:node: \d+\s+cpress=0\.0+\s+))+?(?:frame=\d+\s+)(?:step\s+time=\s+[\w\.\-]+\s+)(?:node: \d+\s+cpress=0\.0+\s+)+(?=frame=\d+)", inputStr, re.S);
#matchedGroup = re.findall("step\s+time=\s+[\w\.\-]+\s+(?:node: \d+\s+cpress=0\.0+\s+)*(?:node:\s+\d+\s+cpress=(?:(?:[1-9]\d*\.\d+)|(?:0\.[1-9]\d*))\s+)(?:(?:node:\s+\d+\s+cpress=(?:(?:[1-9]\d*\.\d+)|(?:0\.[1-9]\d*))\s+)|(?:frame=\d+\s+)|(?:step\s+time=\s+[\w\.\-]+\s+)|(?:node: \d+\s+cpress=0\.0+\s+))+(?:frame=\d+\s+)(?:step\s+time=\s+[\w\.\-]+\s+)(?:node: \d+\s+cpress=0\.0+\s+)+(?:frame=\d+\s+)", inputStr, re.S);
matchedGroup = re.findall("(?:(step\s+time=\s+[\w\.\-]+)\s+)(?:node: \d+\s+cpress=0\.0+\s+)*(?:node:\s+\d+\s+cpress=(?:(?:[1-9]\d*\.\d+)|(?:0\.[1-9]\d*))\s+)(?:(?:node:\s+\d+\s+cpress=(?:(?:[1-9]\d*\.\d+)|(?:0\.[1-9]\d*))\s+)|(?:frame=\d+\s+)|(?:step\s+time=\s+[\w\.\-]+\s+)|(?:node: \d+\s+cpress=0\.0+\s+))+(?:frame=\d+\s+)(?:(step\s+time=\s+[\w\.\-]+)\s+)(?:node: \d+\s+cpress=0\.0+\s+)+(?:frame=\d+\s+)", inputStr, re.S);
print "matchedGroup=",matchedGroup;
if(matchedGroup):
for eachMatched in matchedGroup:
print '-'*80;
print "eachMatched=",eachMatched;

# matchedGroup= [('step time= 1.0001E-04', 'step time= 5.500E-04')]
# --------------------------------------------------------------------------------
# eachMatched= ('step time= 1.0001E-04', 'step time= 5.500E-04')

if __name__ == "__main__":
step_time_betwen_not_zero_and_zero();```

【总结】