[Tutor] Finding wich word matched in a OR re

Kent Johnson kent37 at tds.net
Thu Nov 10 19:46:59 CET 2005


Bernard Lebel wrote:
> Hello,
> 
> I have compiled this regular expression, with 3 strings. Basically it
> looks like this.
> 
> r'word1|word2|word3'
> 
> The point is to try to match one of these words in a string.
> 
> Now let say I indeed got a match in a string. How can I find out wich
> one, between word1-word2-word3 has the string been matched to?

A successful search returns a match object. You can query the match to find out what it matched; match.group() will give the whole matched string. See
http://docs.python.org/lib/match-objects.html
for details.

Kent

-- 
http://www.kentsjohnson.com



More information about the Tutor mailing list