Regular Expressions Quick Question

Paul McGuire ptmcg at austin.rr.com
Wed Jul 9 11:55:16 EDT 2008


On Jul 9, 2:24 am, "Rajanikanth Jammalamadaka" <rajanika... at gmail.com>
wrote:
> hi!
>
> Try this:
>
> >>> lis=['t','tes','test','testing']
> >>> [elem for elem in lis if re.compile("^te").search(elem)]
>
> ['tes', 'test', 'testing']
>
> Cheers,
>
> Raj
>
>
>
>
>
> On Wed, Jul 9, 2008 at 12:13 AM, Lamonte Harris <pyth0nc0... at gmail.com> wrote:
> > Alright, basically I have a list of words in a file and I load each word
> > from each line into the array.  Then basically the question is how do I
> > check if the input word matches multiple words in the list.
>
> > Say someone input "test", how could I check if that word matches these list
> > of words:
>
> > test
> > testing
> > tested
>
> > Out of the list of
>
> > Hello
> > blah
> > example
> > test
> > ested
> > tested
> > testing
>
> > I want it to loop then check if the input word I used starts any of the
> > words in the list so if I typed 'tes'
>
> > Then:
>
> > test
> > testing
> > testing
>
> > would be appended to a new array.
>
> > I'm unsure how to do this in python.
>
> > Thanks in advanced.
>
> > --
> >http://mail.python.org/mailman/listinfo/python-list
>
> --
> "For him who has conquered the mind, the mind is the best of friends;
> but for one who has failed to do so, his very mind will be the
> greatest enemy."
>
> Rajanikanth- Hide quoted text -
>
> - Show quoted text -

Give the built-in string functions a try before resorting to the re
howitzers:

>>> lis=['t','tes','test','testing']
>>> [elem for elem in lis if elem.startswith("te")]
['tes', 'test', 'testing']

-- Paul




More information about the Python-list mailing list