Python regular expression question

Jim Gallacher jg.lists at sympatico.ca
Sun Jun 8 17:19:03 CEST 2003


DJ wrote:
> Hi
> 
> I cant figure out a re that matches a string in a sentance.
> The string can be anywhere in the sentance
> 
> eg
> 
> "dog"
> 
> I walked my dog today
> 
> Cheers
> Adam
> 
> 

Use search instead of match.

 >>> import re
 >>> s = 'I walked my dog today'
 >>> rexp = re.compile('dog', re.I)
 >>> rexp.match(s)
 >>> rexp.search(s)
<_sre.SRE_Match object at 0x818b490>
 >>> rexp.sub('cat',s)
 >>> 'I walked my cat today'


Jim





More information about the Python-list mailing list