searching for multiple strings in one pass

Mitchell Morris mmorris at mindspring.com
Mon Jul 24 11:00:43 EDT 2000


owen at astroNOJNK.washington.edu.invalid (Russell E. Owen) wrote in <8l9v1k
$1tf0$1 at nntp6.u.washington.edu>:

>In article <8l7db8$v3g$1 at nnrp1.deja.com>, ghost_man at my-deja.com wrote:
>
[deletia]
>>How do I check for several file types in one pass?
>
>Not knowing the details of the data you're parsing, two ways come to 
>mind. There are undoubtedly others.
>
>1) Use regular expressions, i.e. the "re" module. In the example you 
>gave, you could look for:
>      \.(?P<ext>(hpp)|(cpp)|(etc))
>to match the extension. (?P<ext>...) labels the found text; if you call 
>the groupdict() method on the match object, you'll get a dictionary, one 
>of whose keys is "ext" -- the found extension.
>
[deletia]
>
>Regards,
>
>-- Russell

Stealing a page from the other 'P' language, you could construct a list of 
the compiled regex objects and apply the compiled form inside your inner 
loop:

import re
import sys

# Worker bees
def do_hpp(line): print 'hpp:', line
def do_cpp(line): print 'cpp:', line
def do_c(line): print 'c:', line
def do_h(line): print 'h:', line

# Selector
funcs = {
    re.compile('\.hpp'): do_hpp,
    re.compile('\.cpp'): do_cpp,
    re.compile('\.c'): do_c,
    re.compile('\.h'): do_h
}

# Off to work with you
while 1:
    line = sys.stdin.readline()
    if not line: break
    for pat in funcs.keys():
        if pat.search(line):
            funcs[pat](line)
            break





More information about the Python-list mailing list