你可以通過re模塊來做到這一點。這裏模式的順序非常重要。由於'organizations of human rights'
位於'human rights'
之前,因此正則表達式引擎會嘗試首先找到organizations of human rights
這個字符串。如果發現匹配,則它將用[
+匹配+ ]
取代匹配。然後它轉到下一個模式,即human rights
是否通過前一模式找到匹配。現在這個human rights
模式將匹配organizations of human rights
字符串中不存在的所有human rights
字符串。因爲默認情況下regex不會進行重疊匹配。如果你想要正則表達式模式做一個重疊匹配,那麼你需要把模式放在周圍,模式必須被()
(,即捕獲組)包圍。
>>> ex = ['liberty of freedom', 'liberty', 'organizations of human rights', 'human rights']
>>> file = " The american people enjoys a liberty of freedom and there are many international organizations of human rights."
>>> reg = '|'.join(ex)
>>> import re
>>> re.sub('('+reg+')', r'[\1]', file)
' The american people enjoys a [liberty of freedom] and there are many international [organizations of human rights].'