替换python3中的字符串中的整个单词

时间:2017-11-21 20:52:47

标签: arrays regex string replace

我需要帮助只用较长的字符串替换整个单词,考虑它们在字符串中的位置,同时替换的单词也在列表中,所以我们遍历所有这些单词并替换所有单词。 I have tried using this solution但它没有用,我还没有找到解决这个问题的解决办法。

我的代码是:

import re

string1="box unbox boxing boxer box box"
string2="ban banana unban ban ban"
string3="box unbox boxing boxer box box ban banana unban ban ban"

dictionary={"box" , "ban"}

var = "chair"

pat = re.compile(r'\b' + '|'.join(dictionary) + r'\b')

string1 = pat.sub(var, string1)
string2 = pat.sub(var, string2)
string3 = pat.sub(var, string3)

print(string1)
print(string2)
print(string3)

我想要的是“盒子”和“禁止”字样被“椅子”取代,无论它们位于字符串的末尾或开头,或者不管它周围有多少个空格。它应该只替换“盒子”和“禁止”字样。

目前的输出是:

chair unbox chairing chairer chair chair
chair banana unchair chair chair
chair unbox chairing chairer chair chair chair banana unchair chair chair

我希望输出为:

chair unbox boxing boxer chair chair
chair banana unban chair chair
chair unbox boxing boxer chair chair chair banana unban chair chair

2 个答案:

答案 0 :(得分:1)

这可能是你想要的:

import re

strings = ["box unbox boxing boxer box box", "ban banana unban ban ban", "box unbox boxing boxer box box ban banana unban ban ban"]

lst = ["box" , "ban"]

rx = re.compile(r'\b(?:{})\b'.format("|".join(lst)))

strings = [rx.sub('chair', string) for string in strings]
print(strings)

这会产生

['chair unbox boxing boxer chair chair', 'chair banana unban chair chair', 'chair unbox boxing boxer chair chair chair banana unban chair chair']

答案 1 :(得分:0)

您的问题是您的正则表达式构造为

\bbox|ban\b

应该在哪里

\b(box|ban)\b

将相关行更改为

pat = re.compile(r'\b(' + '|'.join(dictionary) + r')\b')