regular expressions, substituting and adding in one step?

Kent Johnson kent at kentsjohnson.com
Tue May 9 14:37:13 EDT 2006


John Salerno wrote:
> Ok, this might look familiar. I'd like to use regular expressions to 
> change this line:
> 
> self.source += '<p>' + paragraph + '</p>\n\n'
> 
> to read:
> 
> self.source += '<p>%s</p>\n\n' % paragraph
> 
> Now, matching the middle part and replacing it with '%s' is easy, but 
> how would I add the extra string to the end of the line? Is it done all 
> at once, or must I make a new regex to match?
> 
> Also, I figure I'd use a group to match the word 'paragraph', and use 
> that group to insert the word at the end, but how will I 'retain' the 
> state of \1 if I use more than one regex to do this?

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"

Kent



More information about the Python-list mailing list