简体   繁体   中英

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?

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:
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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM