【发布时间】:2021-08-19 16:16:44
【问题描述】:
所以,我正在编写 discord bot,我使用 on_message() 来检查消息是否包含禁用词。但是,它在执行过程中给出了类似的错误,所以我尝试创建一个单独的文件来测试错误。这是在 discord bot 中进行类似工作的代码。
from re import search
from re import IGNORECASE
banned_words = (r"N[a-zA-Z0-9]gga")
for banned_word in banned_words:
if search(banned_word, input("> "), IGNORECASE):
print("N-word detected")
这是测试
root@kali:~# python3 test.py
> nigga
N-word detected
> nigga
Traceback (most recent call last):
File "/root/test.py", line 5, in <module>
if search(banned_word, input("> "), IGNORECASE):
File "/usr/lib/python3.9/re.py", line 201, in search
return _compile(pattern, flags).search(string)
File "/usr/lib/python3.9/re.py", line 304, in _compile
p = sre_compile.compile(pattern, flags)
File "/usr/lib/python3.9/sre_compile.py", line 764, in compile
p = sre_parse.parse(p, flags)
File "/usr/lib/python3.9/sre_parse.py", line 948, in parse
p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
File "/usr/lib/python3.9/sre_parse.py", line 443, in _parse_sub
itemsappend(_parse(source, state, verbose, nested + 1,
File "/usr/lib/python3.9/sre_parse.py", line 549, in _parse
raise source.error("unterminated character set",
re.error: unterminated character set at position 0
这里可能出了什么问题?它也不应该循环不止一次,对吗?我想知道它是如何询问 input() 两次的。
【问题讨论】:
标签: python python-3.x regex python-re python-3.9