re.sub and named groups

Paul McGuire ptmcg at austin.rr.com
Wed Feb 11 16:05:53 EST 2009


On Feb 4, 10:51 am, "Emanuele D'Arrigo" <man... at gmail.com> wrote:
> Hi everybody,
>
> I'm having a ball with the power of regular expression

Don't forget the ball you can have with the power of ordinary Python
strings, string methods, and string interpolation!

originalString = "spam:%(first)s ham:%(second)s"
print originalString % { "first" : "foo" , "second" : "" }

prints

spam:foo ham:

with far fewer surprises and false steps.  (Note: Py3K supports this
same feature, but with different interpolation syntax, which I have
not learned yet.)

Book recommendation: Text Processing in Python, by David Mertz (free
online - http://gnosis.cx/TPiP/), in which David advises against
dragging out the RE heavy artillery until you've at least thought
about using ordinary string methods.

-- Paul



More information about the Python-list mailing list