通过正则表达式将字符串拆分为数组,并通过函数python(PRAW)进行过滤

2024-05-26 22:58:07 发布

您现在位置:Python中文网/ 问答频道 /正文

背景:

我正在为/r/Avoid5编写bot,这是关于避免此JavaScript输出的内容:

(()=>[..."RZ"].map(a=>atob(a+"Q==")))()

不管怎样,既然我们已经排除了这个问题,bot应该在注释中检测eE的用法并指出它。但是,有一个例外:url。我必须允许包含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)

这里的问题是:bot的罐装响应在url中包含几个e,这不会触发helpers.naughty()函数,但在前面提到的数组理解中显示为几个违规行为。你知道吗

我主要是一个JavaScript程序员,但我正在尝试涉足其他语言,所以请友好。你知道吗

非常感谢, 布伦丹


Tags: textinrecomhttpurlworld定义

热门问题