Aventinus Aventinus - 10 months ago 51
Python Question

How can I search for a word in a file which is preceded and followed by a space in Python?

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
print("Entries found for word: " + str(counter))

Answer Source

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