to match and not to match

brent mojavekid at earthlink.net
Fri Aug 30 19:16:04 EDT 2002


I'm trying to parse a large directory listing of instances where the
word "Psych" shows up; this code snippet has been working great for
me:

filew = open('directory.txt', 'w')
done = 0

while not done:

        aLine = file.readline()

        if aLine != "":

                if re.search('Psych[^irsmo]', aLine):
                        filew.write(aLine)
                        r = r + 1
                        print '.',
        else:
                done = 1

file.close()

My problem is that there are instances where a match needs to not be a
match ... cases where the word Psych shows up in the same line along
with another instance like Psychiatry, psychology. How can I modify
this search to match on a single Psych,  but not if another instance
of Psych* shows up? Any help would be greatly appreciated.

brent.



More information about the Python-list mailing list