-1

I want to search a file line by line for a particular word but I want to make sure that this particular word is being preceded and followed by a space.

I know that I have to do this using regex. I've been reading this guide but it's confusing to me how to apply both lookahead and lookbehind in the same statement.

How can I do this?

counter = 0
out = open('out.txt', 'w')
with open(original_db, 'r') as db:
    for line in db:
        if re.search(word, line, re.IGNORECASE):
            counter += 1
            out.write(line)
print("Entries found for word: " + str(counter))
Aventinus
  • 1,322
  • 2
  • 15
  • 33

1 Answers1

5

You could add word boundary anchors, \b, to your pattern:

if re.search('\b{}\b'.format(re.escape(word)), line, re.IGNORECASE):

These only match when a word character (a-z, 0-9 or _ characters) is preceded or followed by another, non-word character. This is broader than whitespace; words preceded or followed by punctuation also work.

If you must only match words that are whitespace-separated, then don't use a regular expression; just split on whitespace:

if word in line.lower().split():
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343