Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想知道是否可以在更换时保留一些现有价值。例子:
原来的:
{u'America': u'A'}
更换后:
{u'America': _(u'A')}
u\'[w]\'正确选择值,但是如何保存'A'到替换值?
u\'[w]\'
'A'
使用捕获组:
In [13]: s = "{u'America': u'A'}" In [14]: re.sub(r"(u'[\w]')", r"_(\1)", s) Out[14]: "{u'America': _(u'A')}"
在这里,(...)捕获括号内的内容,\1并将其插入替换字符串中。
(...)
\1