python - 禁用词之间的间距 - 正则表达式 Python

标签 python regex

我目前正在尝试制作一种删除特定单词的检查器。我目前正在处理用户将能够在他们的字母之间添加空格并绕过审查员的想法。

一个例子:

Banned word: Apple
Solution: A p p l e

有没有办法在 Regex 中解决这个问题?我的直接想法是使用类似于:

(a\s*p\s*p\s*l\s*e\s*)

但是我觉得这不是最佳解决方案。

如果有解决办法,请告诉我。谢谢。

编辑:

apples其实并不是禁词,只是比较粗俗的词的占位符。

去掉空格然后比较的想法是不可用的,因为一些无害的词可以这样标记。例如:

"We need a medic, he's hit --> weneedamediche'[shit]" FLAGGED.

最佳答案

希望对您有所帮助。

sentence = 'learn to play with code'
sentence_to_word_list = sentence.split(' ') # spliting sentence to words here
banned_Words = ['to', 'with']   # list of banned words

for index, word in enumerate(sentence_to_word_list): # enumerate is used to track the index of each word
    if word in banned_Words:
        sentence_to_word_list[index] = '-'.join(list(word)) # we can join word here with any character,symbol or number

sentence = ' '.join(sentence_to_word_list) # again joining the list of word to make the whole sentence
print(sentence) # output : learn t-o play w-i-t-h code

关于python - 禁用词之间的间距 - 正则表达式 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56809427/

相关文章:

python - 如何在一小段延迟后逐个打印字符

python - sklearn将文本系列转换为稀疏矩阵,然后缩放数字,然后组合成单个X

javascript - 有什么比我的测试门牌号码的模式更好的正则表达式/正则表达式?

c# - 将规范化的电话号码转换为用户友好的版本

python - 如何在特定日期移动数据框中行的时间戳

python - 添加 ndarray 和在 python 中转换为密集的稀疏矩阵时出现广播错误

php - 多维数组自动子数组初始化功能名称?

javascript - 正则表达式以反斜杠结尾

java - 简单的正则表达式 [a-z] 适用于字母,但适用于 Word 则失败

PHP - 不贪婪的正则表达式仍然有点 'greedy'