背景:
我正在为/r/Avoid5编写机器人,这是关于避免此JavaScript输出的内容:
(()=>[..."RZ"].map(a=>atob(a+"Q==")))()
无论如何,现在我们已经完成了这项工作,机器人应该在评论中检测e
或E
的使用并指出它。但是,有一个例外:URLS。我必须允许其中包含e
的URL(并且只解析他们的链接文本,由Reddit的Markdown版本定义)我有PRAW注释流设置,这样它将迭代新注释,将它们分配给变量comment
。我必须生成所有违规行为,从评论的“短语”中定义为以下其中一个字符之间的字符:
;:.,<>()[]!"'?
所以我编写了以下数组解析:
infractions = [x.strip().replace("e", "-").replace("E", "-") for x in re.split(r"[;:.,<>()\[\]!\"']", comment.body) if helpers.naughty(x)]
将helpers.naughty()
定义为:
def naughty(text):
"""
Identifies whether a text string uses the letter 'e'
"""
# Remove newlines
text = re.sub("\n","",text)
# Turn links into their text
links = r'\[([^\]]+)\]\([^\)]+\)'
comment = re.sub(links, r"\1", text, re.DOTALL)
return ("e" in comment or "E" in comment)
我已经确定helper.naughty()
通过大量试验和错误给出了正确的值,但无论如何,这里有正面和负面的例子:
POSITIVE (returns True):
Hello World
[Hey, you](http://amazon.com)
Sup Dude!
[Hello World](http://google.com) | [Hi You](http://example)
NEGATIVE (returns False):
Hi World
[Hi, you](http://example.com)
Sup Guy!
[Hi World](http://google.com) | [Yo-Yo](http://yahoo.com)
问题在于:机器人的预制响应在URLS中包含多个e
,它不会触发helpers.naughty()
函数,但在上述数组理解中会出现几个违规行为。
我主要是一名JavaScript程序员,但我正试图涉足其他语言,所以请你好。
非常感谢, 布伦丹