to match and not to match

David LeBlanc whisper at oz.net
Fri Aug 30 19:41:04 EDT 2002


Alas, what you ask for can't be realized just by an re expression. RE's are
stateless at this level and you're asking for a state "seen other word" to
control how the re evaluates and that can't be done within an re.

However, you could do your per line test using findall and if the number of
groups found is > 1 then you could skip that line:

psychMatch = re.compile(r"(Psych[^irsmo])") # note '()' in pattern to
activate grouping

psychMatch.findall(aLine)
if psychMatch.groups() > 1: #more then one Psych<something> seen
	continue
.
.
.


NB: It's a good idea to get into the habit of using r"...." to enclose re's.

David LeBlanc
Seattle, WA USA

> -----Original Message-----
> From: python-list-admin at python.org
> [mailto:python-list-admin at python.org]On Behalf Of brent
> Sent: Friday, August 30, 2002 16:16
> To: python-list at python.org
> Subject: to match and not to match
>
>
> 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.
> --
> http://mail.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list