Backreference within a character class

Fredrik Lundh effbot at telia.com
Thu Feb 24 22:56:00 CET 2000


taashlo at sandia.gov wrote:
> 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?

this might work: r"(.)(?!\1).\1"

</F>





More information about the Python-list mailing list