Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用于从字符串中清除禁止字列表的正则表达式_Python_Regex - Fatal编程技术网

Python 用于从字符串中清除禁止字列表的正则表达式

Python 用于从字符串中清除禁止字列表的正则表达式,python,regex,Python,Regex,我在这个链接中遵循公认的答案: 尽管完全按照上述解决方案中描述的代码进行操作,但我似乎无法从字符串中删除字符。我在控制台中没有收到任何错误。有人能指出我做错了什么吗?这是一个可复制的例子。多谢各位 example = "(-) This is an example of a string € which is + not being cleaned // correctly" prohibited_strings = ["(-)","€&quo

我在这个链接中遵循公认的答案:

尽管完全按照上述解决方案中描述的代码进行操作,但我似乎无法从字符串中删除字符。我在控制台中没有收到任何错误。有人能指出我做错了什么吗?这是一个可复制的例子。多谢各位

example = "(-) This is an example of a string € which is + not being cleaned // correctly"
prohibited_strings = ["(-)","€","+","//"]
regex_cleaner = re.compile(r'\b%s\b' % r'\b|\b'.join(map(re.escape, prohibited_strings)))
example = regex_cleaner.sub("", example)

\b
含义取决于上下文,您的字符串中没有一个是“单词”,它们既不以单词字符开头也不以单词字符结尾,因此您为什么要费心在模式中添加单词边界?@WiktorStribiżew Hi Wiktor,这是我整个代码的一小部分。我的真实代码还包括数百个字母单词。我的示例字符串有数千个单词长。这就是我添加边界的原因。上面的解决方案也使用
\b
。好的,我明白了。在这种情况下,不能使用
\b
。请参阅,尝试明确的单词边界。如果答案中列出的解决方案不适合您,您可能需要自定义解决方案。我不明白。如何删除混合了“单词”和“非单词”的列表,例如
禁止的字符串=[“(-”、“€”、“+”、“/”、“fhd”、thoias、“opk”]
?很遗憾,您提供的链接没有回答我的问题。我正在使用Python,不理解该链接中的内容。@WiktorStribiżew edit:我将尝试您提供的上述链接。谢谢。请尝试
r'(?