Mailing List Archive

please help with simple regex
I'm a beginner at Python, so this may be a stupid question:

I have a string, for example, myString="the Potato".
1. Convert it to all lowercase ("the potato")
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.

Any help is greatly appreciated.

thanks,
Vadim



Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
please help with simple regex [ In reply to ]
From: jam <jam@newimage.com>

On Fri, Jul 02, 1999 at 12:35:51AM +0000, viofis@my-deja.com wrote:
>
> I'm a beginner at Python, so this may be a stupid question:
>
> I have a string, for example, myString="the Potato".
> 1. Convert it to all lowercase ("the potato")
> 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.
>
> Any help is greatly appreciated.
>
> thanks,
> Vadim
>

hi.

I don't quite understand what it is you are trying to do. if you want to
take a string and find out if it contains some other string, you probably
don't need to worry about regular expressions at all.

what kind of problem are you trying to solve? do you 'need' to use a
specific solution for some reason?

I tried this:

[~] [9:02pm] [jam@toast-pts/6] % python
Python 1.5.2 (#1, Apr 18 1999, 16:03:16) [GCC pgcc-2.91.60 19981201
(egcs-1.1.1 on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import string
>>> buf = "The Carrot"
>>> validveggies = ("carrot", "corn", "spam")
>>> buf = string.lower(buf)
>>> for w in string.split(buf):
... if w in validveggies:
... print "valid veggie '%s' found." % (w)
...
valid veggie 'carrot' found.

does that help?

regards,
Jeff
--
|| visit gfd <http://quark.newimage.com/>
|| psa member #293 <http://www.python.org/>
|| New Image Systems & Services, Inc. <http://www.newimage.com/>
please help with simple regex [ In reply to ]
From: viofis@my-deja.com

I'm a beginner at Python, so this may be a stupid question:

I have a string, for example, myString="the Potato".
1. Convert it to all lowercase ("the potato")
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.

Any help is greatly appreciated.

thanks,
Vadim



Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
please help with simple regex [ In reply to ]
In article <7lh1h6$t84$1@nnrp1.deja.com>, <viofis@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
please help with simple regex [ In reply to ]
From: aahz@netcom.com (Aahz Maruch)

In article <7lh1h6$t84$1@nnrp1.deja.com>, <viofis@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
please help with simple regex [ In reply to ]
On Fri, Jul 02, 1999 at 12:35:51AM +0000, viofis@my-deja.com wrote:
>
> I'm a beginner at Python, so this may be a stupid question:
>
> I have a string, for example, myString="the Potato".
> 1. Convert it to all lowercase ("the potato")
> 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.
>
> Any help is greatly appreciated.
>
> thanks,
> Vadim
>

hi.

I don't quite understand what it is you are trying to do. if you want to
take a string and find out if it contains some other string, you probably
don't need to worry about regular expressions at all.

what kind of problem are you trying to solve? do you 'need' to use a
specific solution for some reason?

I tried this:

[~] [9:02pm] [jam@toast-pts/6] % python
Python 1.5.2 (#1, Apr 18 1999, 16:03:16) [GCC pgcc-2.91.60 19981201 (egcs-1.1.1 on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import string
>>> buf = "The Carrot"
>>> validveggies = ("carrot", "corn", "spam")
>>> buf = string.lower(buf)
>>> for w in string.split(buf):
... if w in validveggies:
... print "valid veggie '%s' found." % (w)
...
valid veggie 'carrot' found.

does that help?

regards,
Jeff
--
|| visit gfd <http://quark.newimage.com/>
|| psa member #293 <http://www.python.org/>
|| New Image Systems & Services, Inc. <http://www.newimage.com/>