please help with simple regex

Aahz Maruch Aahz.Maruch at p98.f112.n480.z2.fidonet.org
Thu Jul 1 21:07:28 EDT 1999


From: aahz at netcom.com (Aahz Maruch)

In article <7lh1h6$t84$1 at nnrp1.deja.com>,  <viofis at my-deja.com> wrote:
>
>I have a string, for example, myString="the Potato".
>1. Convert it to all lowercase ("the potato")

import string
myString = string.lower(myString)

>2. Match it against a list of strings - "potato,tomato,corn,etc...",
>extract "potato" (if it matches with something on the list), then save
>into myNewString.

If you mean literally a list of strings, here's one approach:

stringList = [ 'potato', 'tomato', 'corn' ]
if myString in stringList :
	myNewString = myString

Of course, a better (more efficient) approach equivalent to above is

stringDict = { 'potato':None, 'tomato':None, 'corn':None }
if stringDict.has_key(myString) :
	myNewString = myString

(I'm pretty sure that's not what you want, but Python's regex is a much
smaller hammer than in Perl, and you should get used to trying out other
approaches.)
--
                      --- Aahz (@netcom.com)

Hugs and backrubs -- I break Rule 6       <*>      http://www.rahul.net/aahz/
Androgynous poly kinky vanilla queer het




More information about the Python-list mailing list