Francesco
Francesco

Reputation: 35

RegEx, find a match in a line and print it

From a file structured like this:

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

[...]

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

I wrote a code that extracts all the delimiters, but also some lines that I don't need. Those lines that I don't need cause my code to not run proprerly. I'd like to save in a new .txt file a line if in that line there's the regular expression " [ a number ]". So, to be more accurate in the extraction, I wrote this code in python (following this answer) using re:

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)  

But, in the 'prova.txt' file I could find only the regular expressions so I have a file with [1], [2], ... [n-1], [n].

Upvotes: 1

Views: 2224

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626738

Your new_file is a list of found matches in the file (that you fill with match.group() + newline).

You may check if there is a \[\d+] match in a line and output the line into the new file:

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

Upvotes: 1

Related Questions