python 正则表达式在定义的模式中获取文本

问题描述 投票:0回答:0

我正在编写一个解析器来从下面给出的输出中提取信息 enter image description here

实际上我需要获取“--”之间的所有三个文本。所以我写了一个正则表达式如下

import re
def parse_ib_write_bw(mystr):
    output = dict()
    # match = re.search('--+\n(\n|.)*?--+', mystr, re.I)
    match = re.search('--+\n(.*)--+(.*)--+(.*)--+', mystr, re.DOTALL)

    if match:
        print(match.groups(1))
        print(match.groups(2))
        print(match.groups(3))

parse_ib_write_bw(my_str)

我的理解是:

--+\n(.*)--+    -->  This would give the output of the first block  until second  '---' is found

(.*)--+ --> would give the second block until the third '--' is found 

(.*)--+ --> would give the third block until the final'--' is found 

但实际上我得到了整个输出。请帮助我理解我的理解哪里出了问题。

python-3.x regex
© www.soinside.com 2019 - 2024. All rights reserved.