Python正则表达式 - 替换所有单词

时间:2016-04-26 10:47:36

标签: python regex regex-negation

想象一下,你有以下字符串:

"badger randomword badger mushroom mushroom somethingelse"

我想保留“獾”和“蘑菇”这两个词,并用“蘑菇”取代所有其他词:

"badger mushroom badger mushroom mushroom mushroom"

但是我找不到正则表达式来匹配“除了獾和蘑菇之外的所有东西”。你能救我吗?

5 个答案:

答案 0 :(得分:2)

这将有效

(?!\bbadger\b|\bmushroom\b)\b[^\s]+\b

<强> Regex Demo

Python代码

p = re.compile(r'(?!\bbadger\b|\bmushroom\b)\b[^\s]+\b')
test_str = "badger randomword badger mushroom mushroom somethingelse"
subst = "mushroom"
result = re.sub(p, subst, test_str)

<强> Ideone Demo

答案 1 :(得分:2)

执行此任务的简单非正则表达式oneliner将是 -

new_string = ' '.join(word if word == 'badger' else 'mushroom' for word in original_string.split())

我建议不要在Python中使用复杂的正则表达式,因为Beautiful Is Better Than Ugly

答案 2 :(得分:1)

您可以将re.sub与函数参数一起使用,这将使模式非常简单:

import re

s = "badger randomword badger mushroom mushroom somethingelse"
re.sub('\w+', lambda x: 'badger' if x.group(0) == 'badger' else 'mushroom', s)

答案 3 :(得分:0)

非正则表达式解决方案(并且更容易阅读)是这样的:

oldstring = "badger randomword badger mushroom mushroom somethingelse"
newstring = ""

for part in oldstring.split(" "):
    if part in ["badger", "mushroom"]:
        newstring += part
    else:
        newstring += "mushroom"
    newstring += " "

newstring = newstring[:-1]

答案 4 :(得分:0)

试试这个

(?!badger\b)\b\w+\b

Regex demo

<强>解释
(?!…):否定前瞻sample
\:逃脱一个特殊字符sample
\w:“单词字符”:ASCII字母,数字或下划线sample
+:一个或多个sample

的Python

import re
p = re.compile(ur'(?!badger\b)\b\w+\b')
test_str = u"badger randomword badger mushroom mushroom somethingelse"
subst = u"mushroom"

result = re.sub(p, subst, test_str)#badger mushroom badger mushroom mushroom mushroom