Keywords searching via regexp

Mikael Olofsson mikael at isy.liu.se
Fri Oct 27 10:10:30 EDT 2000


On 27-Oct-00 Eugene A.Tyurkin wrote:
 >  Hi!
 >  Here's a task
 >  I have a list of keywords: keywords = ['qwerty','is','a','good','keyboard']
 >  and want to find any of them in some text
 >  The question is: how to write correct regexp to find them?
 >  Notice, that I don't want to find words like 'goody', I want exectly my
 >  keywords
 >  But I also want to find strings like 'qwerty', #good# etc
 >  Here what I've tryed:
 >  
 >  regexp = r"[^w]*\s+(" + string.join(keywords.'|') + r")"[^\w]*\s+"
 >  aaa = re.compile(regexp)
 >  ...
 >  
 >  It works fine, but it never find the word at the beginnig or the end of
 >  line.
 >  Is it possible to make it all in one?

I know nada about regular expressions, but perhaps you can do the 
following. Make sure that no keyword is exactly at the beginning or at
the end of your text by adding a space in both the beginning and the 
end of the text.

/Mikael

-----------------------------------------------------------------------
E-Mail:  Mikael Olofsson <mikael at isy.liu.se>
WWW:     http://www.dtr.isy.liu.se/dtr/staff/mikael               
Phone:   +46 - (0)13 - 28 1343
Telefax: +46 - (0)13 - 28 1339
Date:    27-Oct-00
Time:    16:07:51

         /"\
         \ /     ASCII Ribbon Campaign
          X      Against HTML Mail
         / \

This message was sent by XF-Mail.
-----------------------------------------------------------------------




More information about the Python-list mailing list