regular expressions, substituting and adding in one step?

John Salerno johnjsal at NOSPAMgmail.com
Tue May 9 21:21:04 CEST 2006


Kent Johnson wrote:

> Do it all in one match / substitution using \1 to insert the value of 
> the paragraph group at the new location:
> 
> In [19]: test = "self.source += '<p>' + paragraph + '</p>\n\n'"
> 
> In [20]: re.sub(r"'<p>' \+ (.*?) \+ '</p>\n\n'", r"'<p>%s</p>\n\n' % 
> \1", test)
> Out[20]: "self.source += '<p>%s</p>\n\n' % paragraph"

Interesting. Thanks! I was just doing some more reading of the re 
module, so now I understand sub() better. I'll give this a try too. Call 
me crazy, but I'm interested in regular expressions right now. :)



More information about the Python-list mailing list