views:

58

answers:

2

I am quite new at python and regex so please bear with me. I am trying to read in a file, match a particular name using a regex while ignoring the case, and store each time I find it. For example, if the file is composed of Bill bill biLl biLL, I need to store each variation in a dictionary or list. Current code:

import re
import sys
import fileinput
if __name__ == '__main__':
    print "flag"
    pattern = re.compile("""([b][i][l][l])""")
    for line in fileinput.input():
        variation=set(pattern.search(line, re.I))

    print variation.groupdict()
    print "flag2"

When ran, the code will return an error: 'NoneType' cannot be iterated (or something along those lines). So how do I store each variation?

Thanks in advance!

+1  A: 

I think that you want re.findall. This is of course available on the compiled regular expression as well. The particular error code that you are getting though, would seem to indicate that you are not matching your pattern. try

pattern = re.compile("bill", re.IGNORE_CASE)
aaronasterling
+2  A: 

I'd use findall:

re.findall(r'bill', open(filename).read(), re.I)

Easy as pie:

>>> s = 'fooBiLL bill BILL bIlL foo bar'
>>> import re
>>> re.findall(r'bill', s, re.I)
['BiLL', 'bill', 'BILL', 'bIlL']
guidoism
so should I use `variation=set(pattern.findall(line, re.I))`I am using the set so there are no duplicates.
amazinghorse24
yup, that works.
guidoism