python3 正则表达式 findall 在 MULTILINE 上工作

python3 regex findall working on MULTILINE

提问人:Tom Ma 提问时间:11/14/2023 最后编辑:Tom Ma 更新时间:11/14/2023 访问量:29

问:

我很难在 python 中编写正则表达式来执行 findall(),并找出所有匹配的文本,不知何故,它没有按照我想要的方式工作。在这种情况下,有人可以帮我弄清楚如何编写正确的正则表达式吗? 另外,为什么 re.compile(r'^all:.*?\n$', re.多托尔 |再。MULTILINE) 得到 2 匹配?

try_findall.py

#!/usr/bin/env python3

import re

# this returns only 2 matches, expect to return 3 matches
allRe = re.compile(r'^all:.*?\n$', re.DOTALL | re.MULTILINE)

# this returns 3 matches, but only matchs till create_links\n, it is expected to match all the texts before next all target
allRe1 = re.compile(r'^all:.*?$\n', re.DOTALL | re.MULTILINE)

with open('targets.inc', 'r') as f:
    line = f.read()
    # print(line)

    all_recipes = allRe.findall(line)
    # all_recipes = allRe1.findall(line)    
    print(len(all_recipes))
    
    for recipe in all_recipes:
        print(recipe)
        print('*'*30 + '\n')

目标.inc

ifeq ($(TARG),MULTI_CMD)
all: create_links
    cd ../ 
export EXTRA_OVERRIDES += MULTI_CMD_SRC=1 FRAME_NUM=21 START_FRAME_NUM=100
else ifeq ($(TARG),MULTI_CMD_UPDATE_PPS)
all: create_links
    cd frame_files;\
export EXTRA_OVERRIDES += MULTI_CMD_SRC=2 FRAME_NUM=18 START_FRAME_NUM=3

else ifeq ($(TARG),MULTI_CMD_MULTI_SLICE_CABAC)
all: create_links
    cd frame_files;\
export EXTRA_OVERRIDES += MULTI_CMD_SRC=6 FRAME_NUM=3 SLICE_NUM=4 START_FRAME_NUM=0
endif
蟒蛇-RE

评论


答: 暂无答案