Backreference within a character class

Michal Wallace (sabren) sabren at manifestation.com
Thu Feb 24 17:15:50 EST 2000


On 24 Feb 2000 taashlo at sandia.gov wrote:

> For the Python RE gurus:
> 
> Using the re module, lets say that I want to match "XIX" but not "XXX"
> or "WOW" but not "WWW".  In my first attempt I used r"(.)([^\1])\1".
> This, of course, did't work because the "\1" in character class isn't
> interpreted as a backreference.
> 
> So is it possible to specify a regular expression to match these
> patterns?


heh.. yup...

>>> import re
>>> r = re.compile(r'(.)(?!\1).\1')
>>> r.match("wow")
<re.MatchObject instance at 79eb20>
>>> r.match("www")
>>>


That was fun.. :)

The (?!..) syntax means "don't match this", but it doesn't move
the little pointer thingy forward.. so "(?!\1)" tells it to make
sure the next thing doesn't match \1, and then the "." says
to match any character (which we already know can't be \1)..

but also, if you're only dealing with 3 character strings:

>>> r = lambda s: s[0]==s[2]!=s[1]
>>> r("wow")
1
>>> r("www")
0
>>> 

(aawwww.. my first lambda..)

Cheers,

- Michal
-------------------------------------------------------------------------
http://www.manifestation.com/         http://www.linkwatcher.com/metalog/
-------------------------------------------------------------------------





More information about the Python-list mailing list