代码之家  ›  专栏  ›  技术社区  ›  om tripathi

查找两个字符串模式之间的文本

  •  -3
  • om tripathi  · 技术社区  · 6 年前

    data = '''
    ================================================================================
    Annotation file:
    /b/cd-builder/sandboxes/sb_DEV_COMMON_BRANCH-_act-builder-4977/annofile-4977TB53661_cd-builder.xml 
    
    Build ID: 27951 on Host bng-emake-5a.juniper.net, Cluster Manager: bng-ea-cm-02:8030
    Start Time: Sun Jun 17 23:57:30 2018
    
    
    Job ID: J00002adde8677420 , Exit Value 1
    CWD: /b/cd-builder/sandboxes/sb_DEV_COMMON_BRANCH-_act-builder-4977/obj/bsd11/amd64/junos/usr.sbin/rpd/bgp/lib/proto
    Node: bng-ea-agent-14a-3  Start: 2018-06-18 00:20:55.488609 (1405.488609)
                              End:   2018-06-18 00:20:56.653945 (1406.653945)
    
    Command:
    
    export BMAKELOCATION=/b/cd-builder/sandboxes/sb_DEV_COMMON_BRANCH-_act-builder-4977/src/build/mk/jnx.sym_check.mk:37; CURDIR=/b/cd-builder/sandboxes/sb_DEV_COMMON_BRANCH-_act-builder-4977/src/junos/usr.sbin/rpd/bgp/lib/proto  OBJDUMP=/volume/hab/Linux/Ubuntu-12.04/x86_64/llvm/3.7/current/bin/amd64-unknown-freebsd11.0-objdump  BSS_SYMS=bss_syms.clang.amd64,bsdx  TLS_SYMS=tls_syms.clang.amd64,bsdx  ACCEPT_CMD='mk --machine amd64,bsd11 -C junos/usr.sbin/rpd/bgp/lib/proto accept-syms'  /volume/hab/Linux/Ubuntu-12.04/x86_64/bsd-tools/current/bin/sh /b/cd-builder/sandboxes/sb_DEV_COMMON_BRANCH-_act-builder-4977/src/build/scripts/sym_check.sh librpd-proto-bgp.a
    
    ------------------------------ Output ------------------------------
    ERROR: /b/cd-builder/sandboxes/sb_DEV_COMMON_BRANCH-_act-builder-4977/src/junos/usr.sbin/rpd/bgp/lib/proto/tls_syms.clang.amd64,bsdx: changed global or static TLS variables
    261a262
    > bgp_inetsrte_incolor_update._xinfo
    
    If correct, use:
    
        mk --machine amd64,bsd11 -C junos/usr.sbin/rpd/bgp/lib/proto accept-syms
    
    make[1]: *** [check-syms] Error 1
    --------------------------------------------------------------------
    
    Operations:
    
    ================================================================================
    + echo -e '\e[31m Displaying Production.log.errs\e[0m'
     Displaying Production.log.errs
    + echo 'End Meta Error logs'
    End Meta Error logs
    '''
    

    output = '''
    ERROR: /b/cd-builder/sandboxes/sb_DEV_COMMON_BRANCH-_act-builder-4977/src/junos/usr.sbin/rpd/bgp/lib/proto/tls_syms.clang.amd64,bsdx: changed global or static TLS variables
    261a262
    > bgp_inetsrte_incolor_update._xinfo
    
    If correct, use:
    
        mk --machine amd64,bsd11 -C junos/usr.sbin/rpd/bgp/lib/proto accept-syms
    
    make[1]: *** [check-syms] Error 1
    '''
    

    不知道为什么我下面的代码不起作用。

    for result in re.findall(r'------------------------------ Output ------------------------------(.*?) '
                             '--------------------------------------------------------------------', data, re.S):
        output += result
    
    5 回复  |  直到 6 年前
        1
  •  2
  •   Rakesh    6 年前

    Lookbehind & Lookahead

    import re
    for result in re.findall(r'(?<=------------------------------ Output ------------------------------)(.*?)(?=--------------------------------------------------------------------)', data, re.S):    
        print(result)
    
        2
  •  1
  •   Mathieu    6 年前

    start = "------------------------------ Output ------------------------------"
    stop = "--------------------------------------------------------------------"
    output = log.split(start)[1].split(stop)[0]
    
        3
  •  1
  •   Negi Babu    6 年前
    output = ""
    for result in re.findall(r'------------------------------ Output ------------------------------(.*?)'
                         '--------------------------------------------------------------------', data, re.S):
    output += result
    
    print output
    

    (.*?)

        4
  •  1
  •   AndreyF    6 年前

    你不需要匹配regex就可以做到这一点。你可以用 find

    start = "------------------------------ Output ------------------------------"
    end = "--------------------------------------------------------------------"
    
    extracted_text = data[data.find(start)+len(start):data.rfind(end)] 
    

    /b/cd builder/sandbox/sb_dev_common_branch-_act-builder-4977/src/junos/usr.sbin/rpd/bgp/lib/proto/tls_syms.clang.amd64,bsdx:

    mk --machine amd64,bsd11 -C junos/usr.sbin/rpd/bgp/lib/proto accept-syms
    

    生成[1]:***[检查符号]错误1

        5
  •  -1
  •   kantal    6 年前

    reg=re.compile(r"-{10,} Output -{10,}(.*?)-{10,}",re.S)
    rslt=reg.findall(data)