Module RE, Have a couple questions

Francis Girard francis.girard at free.fr
Tue Mar 1 20:57:43 CET 2005


Le mardi 1 Mars 2005 16:52, Marc Huffnagle a écrit :
> [line for line in document if (line.find('word') != -1 \
>         and line.find('wordtwo') != -1)]

Hi,

Using re might be faster than scanning the same line twice :

=== begin snap
## rewords.py

import re
import sys

def iWordsMatch(lines, word, word2):
  reWordOneTwo = re.compile(r".*(%s|%s).*" % (word,word2))
  return (line for line in lines if reWordOneTwo.match(line))
  
for line in iWordsMatch(open("rewords.py"), "re", "return"):
  sys.stdout.write(line)
=== end snap

Furthermore, using list comprehension generator (2.4 only I think) and file 
iterator, you can scan files as big as you want with very little memory 
usage.

Regards,

Francis Girard




More information about the Python-list mailing list