我有一个这样的正则表达式:
findthe = re.compile(r" the ")
replacement = ["firstthe", "secondthe"]
sentence = "This is the first sentence in the whole universe!"
我要做的是用列表中的一个相关的替换词替换每次出现的情况,这样结尾的句子看起来像这样:
^{pr2}$
我尝试在for循环中使用re.sub
对替换进行枚举,但看起来re.sub
返回所有出现的情况。有人能告诉我如何有效地做到这一点吗?在
Tags:
可以使用回调函数作为替换参数,请参见如何在:
http://docs.python.org/library/re.html#re.sub
然后使用一些计数器并根据计数器值进行更换。在
{artm>的最后一个变量是破坏性的。这里有一种不清空
replacement
的方法如果不需要使用regEx,则可以尝试使用以下代码:
或者类似这样的方法也会起作用:
^{pr2}$至少:
相关问题 更多 >
编程相关推荐