searching for matches within a word list
Peter Abel
p-abel at t-online.de
Wed Jul 30 06:42:46 EDT 2003
Rajarshi Guha <rajarshi at presidency.com> wrote in message news:<pan.2003.07.29.21.35.57.62265.14802 at presidency.com>...
> Hi,
> I have a large list of words (each on its own line)
> and I would like to find words that contain a specific string.
>
> I have been trying to use a regexp but I cant see how I can find the
> word that contained the regex pattern - all I get is the pattern itself
> if I use match or findall.
>
> I know that I could just go through the list line by line and see if the
> regex matches or not - but that method seems horifically inefficient.
>
> Does anybody have any suggestions as to how I could solve this
> efficiently?
>
> Thanks,
> Rajarshi
Suppose your words are in one string delimited by newline:
>>> import re
>>> words='MyPython\nJohn\nPython\nMary\nJython\nNothing'
>>> # Suppose youn want to find all the words with the substing "ython" in them.
>>> pattern='(.*?ython.*?)'
>>> re.findall(pattern, words)
['MyPython', 'Python', 'Jython']
>>>
Hope this helps
Regards Peter
More information about the Python-list
mailing list