我要匹配Something (匹配 然后要从中提取“某样词并将其保存在列表中)

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

!!!附有代码部分的文件avengers.txt已被读取,在读取下一行之前!!

我想匹配(,,)并将它们从(钢铁侠,怪异,混蛋)中排除,并保存(钢铁侠,怪异,混蛋)在列表中

我用r'<(.*alive)>1来匹配,同样用于除尘和死亡。现在我想从中分离iron man并将其保存在列表中,并从中分离docter strange并将其保存在单独的列表中,并与gamora]相同

我用来匹配存活,尘土飞扬和死亡的代码:

#!/usr/bin/env python3

import re

#file = open("avengers.txt", "r")

def alive():
    with open("avengers.txt") as f:
        for i in f:
            rx = re.findall("<(.*alive)>", i)
            print(rx)


def died():
    with open("avengers.txt") as f:
        for i in f:
            rx = re.findall("<(.*died)>", i)
            print(rx)

def dusted():
    with open("avengers.txt") as f:
        for i in f:
            rx = re.findall("<(.*dusted)>", i)
            print(rx)


-----avengers.txt-------
iron man<alive>
doctor strange<dusted>
gamora<died>
------------------------


````````

!!!文件avengers.txt附带有代码部分,请先阅读,然后再阅读下一行!!!我想匹配(,,)并将它们从(钢铁侠,怪异的家伙,gamora)中排除,然后保存(钢铁侠,怪异的家伙...

python regex python-3.x regex-lookarounds regex-group
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.