[Tutor] Why doesn't this regex match???

Sheila King sheila@thinkspot.net
Fri, 08 Feb 2002 22:21:28 -0800


On Fri, 08 Feb 2002 21:53:49 -0800, Kirby Urner <urnerk@qwest.net>
wrote about Re: [Tutor] Why doesn't this regex match???:

> 
> > >>> p = re.compile(r'\b%s\b' % pattern)
> > >>> result = p.search(searchstring, re.IGNORECASE)
> 
> One thing here is the re.I or re.IGNORECASE parameter is
> compile time, i.e. goes in the compile, not in the search.

Oh, oops! OK, taking this advice, I re-did my previous tests:

>>> searchstring = 'ADV: FREE FREE OFFERZ!!!!'
>>> pattern = 'adv:'
>>> p = re.compile(r'\b%s\b' % pattern, re.IGNORECASE)
>>> result = p.search(searchstring)
>>> print result
None
>>> searchstring = 'Viagra without a prescription!'
>>> pattern = 'viagra'
>>> p = re.compile(r'\b%s\b' % pattern, re.IGNORECASE)
>>> result = p.search(searchstring)
>>> print result
<_sre.SRE_Match object at 0x00F476F0>
>>> searchstring = 'get viagra without a prescription!'
>>> pattern = 'viagra'
>>> p = re.compile(r'\b%s\b' % pattern, re.IGNORECASE)
>>> result = p.search(searchstring)
>>> print result
<_sre.SRE_Match object at 0x00F3A5F0>
>>> 

Now two out of three match as I had expected, and one at the beginning
of the string. Why isn't the first one matching, still?

-- 
Sheila King
http://www.thinkspot.net/sheila/

"When introducing your puppy to an adult cat,
restrain the puppy, not the cat." -- Gwen Bailey,
_The Perfect Puppy: How to Raise a Well-behaved Dog_