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

Sheila King sheila@thinkspot.net
Fri, 08 Feb 2002 23:26:53 -0800


On Fri, 08 Feb 2002 21:30:15 -0800, Sheila King <sheila@thinkspot.net>
wrote about [Tutor] Why doesn't this regex match???:

> OK, I'm having some trouble with using the re module for regular
> expression matching. (I'm very new to using regular expressions, so I
> suppose I could be doing something really stupid?)

Despite Tim's excellent advice, to reconsider whether I really want to
use regular expressions, I persist at that task. (I've been programming
long enough to feel that I ought to have learned them by now. And I'd
like to start understanding them so when other people discuss them I
don't feel lost.)

So, I decided I could possibly solve my problem with the whole "word
boundary" thing (when my phrase-pattern doesn't begin and end in an
alphanumeric character) as follows:

Match on any of the following at the beginning of the phrase-pattern:

The beginning of the searched string
A word boundary
White space

and any of the following at the end of the phrase-pattern:

The end of the searched string
A word boundary
White space

Seems to me, that this should about cover things???

So I tried the following, with the dismal results shown. Now what am I
doing wrong? 

>>> searchstring = 'ADV: FREE FREE OFFERZ!!!!'
>>> word = 'adv:'
>>> p = re.compile(r'[\b\A\s]%s[\b\Z\s]' % word, re.I)
Traceback (most recent call last):
  File "<pyshell#45>", line 1, in ?
    p = re.compile(r'[\b\A\s]%s[\b\Z\s]' % word, re.I)
  File "E:\PYTHON\PYTHON22\lib\sre.py", line 178, in compile
    return _compile(pattern, flags)
  File "E:\PYTHON\PYTHON22\lib\sre.py", line 228, in _compile
    raise error, v # invalid expression
error: internal: unsupported set operator

Note: it doesn't matter whether I make it a raw string or not. Same
error both ways. Clearly something isn't right with the pattern I'm
trying to use.

-- 
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_