Despite the often pronounced antipathy for regular expressions on the part of many in the Python community, they are indeed a valuable tool for relevant use cases, which certainly include the identification of words and phrases (thanks to the \b“word boundary element” in regular expression patterns - alternatives to the basis of string processing are much more complex tasks, for example, .split()using spaces as a separator and, therefore, annoyingly leaves punctuation marks on adjacent words attached to them, etc. etc., etc.).
If the RE is fine, I would recommend something like:
import re
import sys
def main():
if len(sys.argv) != 3:
print("Usage: %s fileofstufftofind filetofinditin" % sys.argv[0])
sys.exit(1)
with open(sys.argv[1]) as f:
patterns = [r'\b%s\b' % re.escape(s.strip()) for s in f]
there = re.compile('|'.join(patterns))
with open(sys.argv[2]) as f:
for i, s in enumerate(f):
if there.search(s):
print("Line %s: %r" % (i, s))
main()
, () , , () , . , , (, ) .. ..
, RE...:
\b patterns , ( "" "", "" "", ", , " , , "", , -).
| or, , , ( )
cat
dog
'\bcat\b|\bdog\b', "", "" ( , , ).
re.escape , , , RE.