如何从文本文件中打印包含关键字和关键字之后的文本?

时间:2020-07-17 06:58:33

标签: python python-3.x regex

我正在尝试在特定字符串之后打印文本。

file.txt

I am: "eating", mango
I am: eating a pine apple; and mango

我正在尝试编写代码,在该代码中应搜索关键字am:并在“”中打印文本。如果上午之后的行中没有“”:那么我想打印到;(或简单地说3个字)。

output.txt

I am: eating
I am: eating a pine apple

我的工作:

with open('input.txt', 'r') as f, open ("output.txt", 'w') as out_fh:
    for line in f:
        str = re.search(r'\bam: "([^"]+)"', line).group()[0]
        if str:
            out_fh.write(str)
        else:
            a = re.compile(r'am:((\w+){3}')
            out_fh.write(a)

不确定我要去哪里。任何帮助,将不胜感激。谢谢

1 个答案:

答案 0 :(得分:1)

您可以使用单个正则表达式获取预期结果:

rx = re.compile(r'^(I am:\s*)("[^"]*"|[^;]*)')

请参见regex demo。正则表达式匹配

  • ^-字符串的开头
  • (I am:-捕获组1的开始:I am:字符串
  • \s*)-空格+0,捕获组1结束
  • ("[^"]*"|[^;]*)-捕获组1:一个",后跟"以外的0个或多个字符,然后是",或者{以外的0+个字符{1}}

在您的代码中,像这样使用它:

;

请注意,rx = re.compile(r'\bam:\s*("[^"]*"|[^;]*)') with open('input.txt', 'r') as f, open ("output.txt", 'w') as out_fh: for line in f: m = rx.search(line) if m: out_fh.write( "{}{}".format(m.group(1), m.group(2).strip('"')) ) 将删除在组1中用第一个备用字符捕获的前.strip('"')个字符。

查看Python demo

"

输出:

import re
text = """I am: "eating", mango
I am: eating a pine apple; and mango"""
rx = re.compile(r'^(I am:\s*)("[^"]*"|[^;]*)')
for line in text.splitlines():
    m = rx.search(line)
    if m:
        print("{}{}".format(m.group(1), m.group(2).strip('"')))
相关问题