How to know what re.sub took?

Fredrik Lundh effbot at telia.com
Wed Apr 12 15:27:50 CEST 2000


Per Kistler <kistler at gmx.net> wrote:
> How to know what has been substituted away, if one does something like:
>
> >>> import re
> >>> rex = re.compile(r"(hans|fred|simon)")
> >>> st = "max hans bill"
> >>> st2 = rex.sub("",st)
> >>> st2
> 'max bill'
>
> It took "hans" away, but how can I learn this automatically?

>>> st2 = rex.subn("",st)
>>> st2
('max  bill', 1)

the second tuple value is the number of substitutions made.

</F>





More information about the Python-list mailing list