re documentation error

Chris Gonnerman chris.gonnerman at newcenturycomputers.net
Mon Sep 17 08:18:26 EDT 2001


----- Original Message -----
From: "Carlos Gaston Alvarez" <cgaston at moonqzie.com>


> Empty matches are replaced only when not they're not adjacent to a
previous
> match.

I don't understand how this ^ has anything to do with this v

> >>> p = re.compile('x*')
> >>> p.sub('-', 'abxd')
> '-a-b-d-'

You have an expression matching zero or more x's, and you are substituting
a dash.  This result is exactly right.

> I would expect the result to be.
> 'abd'

Why?  You are putting a dash into the string.  If you had said you expected
the result to be 'ab-d' I would know you didn't understand the expression,
but evidently you do.  Do you think that a dash in the *substitution* string
means something special?  With the exception of backslash-escapes, there is
*nothing* special about that string.

> If the '-' is representing no char, an empty string (as the text says)
then

The text (2.1 is what I am looking at) says nothing of the sort where the
example you show is described.  It says, to wit:

    Empty matches for the pattern are replaced only when not adjacent to a
    previous match, so "sub('x*', '-', 'abc')" returns '-a-b-c-'.

Are you mixing this up with the example a few paragraphs prior?  That
example
is using a *function* for the replacement value and has nothing to do with
the rule you are complaining of.

> I would like it to say
> >>> p = re.compile('x*')
> >>> p.sub('', 'abxd')
> 'abd'
>
> Which is an example that does teachs nothing new.
>
> is - an special char of re for representing nothing?
> Dont think so.

In fact, it's not.  The example doesn't say it is.

> Chau,
>
> Gaston
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>





More information about the Python-list mailing list