Natasha
Natasha

Reputation: 1521

Filter lines that contains exact match of a string

I am trying to filter the lines in a file that contains exact match of a string, in python. To begin with, I've written the following section of the code,

lines = ["z  = xxx;  %eflee","x  = eee ;  %flee"]

for lineNo,line in enumerate(lines):
    tags = ["flee"]
    for tag in tags:
        if tag in line:
           print(line)

Desired Output:

x  = eee ;  %flee

Obtained output:

z  = xxx;  %eflee
x  = eee ;  %flee

Could someone explain how to get the desired output?

Upvotes: 0

Views: 1304

Answers (2)

CryptoFool
CryptoFool

Reputation: 23089

Here's how to match each of your tags as a whole word only:

import re

lines = ["z  = xxx;  %eflee","x  = eee ;  %flee"]

for lineNo,line in enumerate(lines):
    tags = ["flee"]
    for tag in tags:
        if re.search(r'\b%s\b' % tag, line):
           print(line)

Output:

x  = eee ;  %flee

r'\bflee\b' is a regular expression that matches only the whole word 'flee'. It works because '\b' means to only match at a word boundary.

If it's possible that you could match two tags in a single line, and you don't want to then print the same line twice, you should put "break" under "print(line)" at the same indentation level.

You don't use lineNo. If you really don't need it, you can change your 'for' line to:

for line in lines:

Upvotes: 1

Happy Boy
Happy Boy

Reputation: 536

import re


lines = ["z  = xxx;  %eflee","x  = eee ;  %flee"]
tags = ["flee"]
pattern = "|".join([" %{} ?".format(t) for t in tags])
# pattern: ' %flee ?'
regex = re.compile(pattern)

for lineNo,line in enumerate(lines):
    if regex.search(line):
        print(line)

Upvotes: 1

Related Questions