[Tutor] substituting with a group
Gregor Lingl
glingl@aon.at
Mon, 8 Jul 2002 07:09:06 +0200
for instance:
>>> import re
>>> s="(the quick (brown fox"
>>> p = re.compile('\((\w+)')
>>> p.sub(r'<\1>',s)
'<the> quick <brown> fox'
>>>
... maybe there are more elegant ways.
Gregor
----- Original Message -----
From: "Paul Tremblay" <phthenry@earthlink.net>
To: <tutor@python.org>
Sent: Monday, July 08, 2002 6:35 AM
Subject: [Tutor] substituting with a group
> Given the following line:
>
> {the quick {brown fox"
>
> I want to produce the following line:
>
> <the> quick <brown> fox
>
> I know that in perl, you use an expression like this:
>
> $string =~ s/\{(\w+)/<$1>/g;
>
> In other words, I want to substitute using the group generated
> from the search. How do you do this in python?
>
> (Core Python didn't give me any hint.)
>
> Thanks!
>
> Paul
>
>
>
> --
>
> ************************
> *Paul Tremblay *
> *phthenry@earthlink.net*
> ************************
>
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>