python - RegEx,在一行中找到匹配项并打印它

标签 python regex python-3.x

来自如下结构的文件:

..............................
Delimiter [1]
..............................
blablabla
..............................
Delimiter CEO [2]
..............................
blabla
..............................
Delimiter [3]
..............................

[...]

..............................
Delimiter CEO [n-1]
..............................
blablabla
..............................
Delimiter [n]
..............................   

我编写了一个代码来提取所有分隔符,但也提取了一些我不需要的行。我不需要的那些行 my code无法正常运行。 我想在新的 .txt 文件中保存一行,如果该行中有正则表达式“[a number]”。因此,为了更准确地提取,我使用 re: 在 python 中编写了这段代码(在 this answer 之后):

import re
with open('testoestratto.txt','r',encoding='UTF-8') as myFile:
    text = myFile.readlines()
    text = [frase.rstrip('\n') for frase in text]
    regex = r'\[\d+\]'
    new_file=[]
    for lines in text:
       match = re.search(regex, lines, re.MULTILINE)
       if match:            
           new_line = match.group() + '\n'            
           new_file.append(new_line)

with open('prova.txt', 'w') as f:     
     f.seek(0)    
     f.writelines(new_file)  

但是,在“prova.txt”文件中,我只能找到正则表达式,因此我有一个包含 [1]、[2]、... [n-1]、[n] 的文件。

最佳答案

您的 new_file 是文件中找到的匹配项的列表(您用 match.group() + 换行符填充)。

您可以检查一行中是否有 \[\d+] 匹配并将该行输出到新文件中:

import re

reg = re.compile(r'\[\d+]') # Matches a [ char, followed with 1+ digits and then ]

with open('prova.txt', 'w') as f:     # open file for writing
    with open('testoestratto.txt','r',encoding='UTF-8') as myFile: # open file for reading
        for line in myFile:           # read myFile line by line
            if reg.search(line):      # if there is a match anywhere in a line
                f.write(line)         # write the line into the new file

关于python - RegEx,在一行中找到匹配项并打印它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51458478/

相关文章:

python - 聚合一组数据并输出嵌套字典的函数

php - preg_match_all 用于括号内和括号外的单词

python-3.x - Flask 迁移失败

python - 如何减去带有 float 的变量?

python - pandas DataFrame 中 bool 数组的按行求和

Python:将 unicode 字符串转换为 MM/DD/YYYY

python - 是否可以从 SQL Server 在 Active Directory 中启动新条目或更新 Active Directory 中的现有条目?

python - pandas:如何将 bin 值追加回原始数据框

c# - Linq2Xml 删除特定属性名称及其值

java - 正则表达式代替 String.contains(xyz) (Java 1.7)