我想匹配 Something<alive>(匹配 <alive> 然后想从中提取“Something word 并将其保存在列表中)

i want to match Something<alive> (match <alive> and then wanna extract "Something word from it and save it in a list)

!!!文件 avengers.txt 附有代码部分,请在阅读下一行之前阅读!!!

我想匹配 (, , ) 并将它们从 (iron man, docter strange, gamora) 中排除并将 (iron man, docter strange, gamora) 保存在列表中

我用 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>
------------------------


````````

正则表达式 (.+)<(.+)> 将匹配文件中的每一行。使用括号允许您 "capture" 匹配的字符串部分,然后使用 .group 方法检索这些部分。

我建议只打开文件一次,将其解析为数据结构,然后查询该数据结构 - 而不是每次查询都打开和解析文件。

import re

def parse_file(filename):
    pattern = re.compile('(.+)<(.+)>')
    results = { 'alive': [], 'dusted': [], 'died': [] }
    with open(filename) as f:
        for line in f:
            m = pattern.search(line)
            if m:
                name = m.group(1)
                status = m.group(2)
                results[status].append(name)
    return results


statuses = parse_file('avengers.txt')

print(statuses['alive'])     # ['iron man']
print(statuses['dusted'])    # ['doctor strange']
print(statuses['died'])      # ['gamora']
alive = []
died = []
dusted = []
with open("avengers.txt") as f:
    for i in f:
        i = i.strip()
        if '<alive>' in i:
            alive.append(i.replace('<alive>', ''))
        if '<died>' in i:
            died.append(i.replace('<died>', ''))
        if '<dusted>' in i:
            dusted.append(i.replace('<dusted>', ''))

print('****alive****')
print(str(alive))
print('****died****')
print(str(died))
print('****dusted****')
print(str(dusted))

avengers.txt

iron man<alive>
iron man_2<alive>
doctor strange<dusted>
doctor strange_2<dusted>
gamora<died>
gamora2<died>