我目前正在尝试制作一种删除特定单词的检查器。我目前正在处理用户将能够在他们的字母之间添加空格并绕过审查员的想法。
一个例子:
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/