[Tutor] Why doesn't this regex match???
Kirby Urner
urnerk@qwest.net
Sat, 09 Feb 2002 01:20:17 -0800
>
>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
At this point, I share your confusion. Using your same values:
>>> p = re.compile(r'\A%s' % word, re.I) # works
>>> p = re.compile(r'[\A]%s' % word, re.I) # finds nothing
>>> p = re.compile(r'[\A]*%s' % word, re.I) # works
>>> p = re.compile(r'[\A]?%s' % word, re.I) # works
? and * both match 0 times, as well as 1 or many respectively.
But if \A is not there at all, why does the first pattern work?
If \A is there, why does the 2nd pattern fail?
Something about p = re.compile(r'[\s\A]') is invalid.
I'm not sure what.
Kirby