最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

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

Python re crifan 3159浏览 0评论

【问题】

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值
https://www.crifan.com/python_re_findall_between_begin_not_zero_and_all_zero_step_time

Author:     Crifan Li
Version:    2013-06-20
Contact:    https://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();

 

【总结】

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

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

转载请注明:在路上 » 【问题解答】用Python的正则去匹配cpress值开始不等于零和重新全部等于零时的step time值

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.354 seconds, using 22.06MB memory