如何在Python代码中实现上述regex

2024-06-28 21:01:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个regex PROTO\s*\{(\n*\s*\w+,)+\n*\s*\}来匹配文本文件,如下所示

PROTO {
    product1,
    product2,
    product3,
    product4,
    product5,
    product6,
}

SAVE_LOG: True

SUMMARY: True

如何使用上面提到的regex将输出转换为

['product1', 'product2', 'product3', 'product4', 'product5', 'product6']


Tags: logtruesavesummaryregexproto文本文件product1
3条回答

如果您能够安装较新的^{}模块(它支持\G修饰符),您可以想出类似(demo on regex101.com)的方法:

(?:^PROTO\s*\{\s+|(?!\A)\G\s*)([^,\n\r]+),

Python中,这将是:

import regex as re

string = """
PROTO {
    product1,
    product2,
    product3,
    product4,
    product5,
    product6,
}

SAVE_LOG: True

SUMMARY: True
"""

rx = re.compile(r"""
        (?:^PROTO\s*\{\s+   # look for PROTO at the beginning of the line,
                            # followed by whitespace and {
            |               # OR
            (?!\A)\G\s*)    # start at the previous match (make sure it's not the start)
        ([^,\n\r]+),        # look for sth. that is not a comma or newline
        """, re.VERBOSE|re.MULTILINE)

matches = rx.findall(string)
print matches
# ['product1', 'product2', 'product3', 'product4', 'product5', 'product6']

它的优点是只有一个regex(另外编译),因此它可能更快。你知道吗

这将获得所需的阵列:

import itertools
protos = re.findall(r'PROTO\s*\{(.*?)\}', data, flags=re.DOTALL)
lines = [re.findall(r'(\w+),', x) for x in protos]
products = list(itertools.chain.from_iterable(lines))

这不需要regex,您可以通过简单的字符串函数实现所需的功能。你知道吗

with open('path/to/file.txt','r') as fp:
    product_list = []
    for line in fp.readlines():
        if line.strip()[:5] == 'PROTO':
            append_bool = True
        elif append_bool and line.find('}')>=0:
            append_bool = False
        if append_bool:
            product_list.append(line.strip().replace(',',''))

相关问题 更多 >