Alex Rivera | Logout

Regex replace (in Python) - a simpler way?

Asked 2009-01-29T05:43:24.357
43

Any time I want to replace a piece of text that is part of a larger piece of text, I always have to do something like:

"(?P<start>some_pattern)(?P<replace>foo)(?P<end>end)"

And then concatenate the start group with the new data for replace and then the end group.

Is there a better method for this?

Edit
Report

1 Answer

18

Look in the Python re documentation for lookaheads (?=...) and lookbehinds (?<=...) -- I'm pretty sure they're what you want. They match strings, but do not "consume" the bits of the strings they match.

answered 2009-01-29T05:51:21.050

Your Answer