Keywords searching via regexp

Alex Martelli aleaxit at yahoo.com
Fri Oct 27 10:39:15 EDT 2000


"Eugene A.Tyurkin" <hackee at penguinpowered.com> wrote in message
news:mailman.972655156.23780.python-list at python.org...
> 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?

Sure, just change the [^w]*\s+ (which means "0+ non-w's" (?) "then one or
more whitespace characters") to \b (which means "word-boundary"), and the
same for the trailing "0 or more non-word-characters then one or more
whitespace characters", [^\w]*\s+, that becomes a \b (word-boundary) too.
(I guess the " right after the close-paren towards the end is a typo...?).


Alex






More information about the Python-list mailing list