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

【问题】

python程序求助

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

cpress_step_time_sample_data

 

【问题解答】

写了python代码,调试了半天,终于达到要的效果了:

#!/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():
    inputStr = open("./input.txt").read();
    #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();

 

【总结】

本来以为不难的,实际上,还是不容易的,也是调试了半天,才写出来的。

不过,这种问题,对于正则,也还是属于那种,稍微花点时间,就可以搞定的问题的。



发表评论

电子邮件地址不会被公开。 必填项已用*标注

无觅相关文章插件,快速提升流量