Reputation: 22449
I was wondering if it's possible to reserve some of an excisting value, while replacing. Example:
Original:
{u'America': u'A'}
After replacing:
{u'America': _(u'A')}
u\'[w]\'
selects the value properly, however how do I save 'A'
to the replacement value?
Upvotes: 2
Views: 622
Reputation: 500317
Use a capture group:
In [13]: s = "{u'America': u'A'}"
In [14]: re.sub(r"(u'[\w]')", r"_(\1)", s)
Out[14]: "{u'America': _(u'A')}"
Here, (...)
captures what's inside the parentheses, and \1
inserts it into the replacement string.
Upvotes: 3