如何创建 python 正则表达式以仅在至少存在 3 个定义的词时匹配,并且还允许其他随机词?

How to create python regex to match only if at least 3 defined words are exist and allow other random words as well?

这是我的例子:

re.compile("({tables}) ({middle})  ({end})"format(tables=config.kvkk_tables,middle=config.middle_sentence,end=config.end_of_sentence),re.IGNORECASE))

其中一个表格,中间和结尾字符串必须在句子中。当这个条件匹配时,我们不想检查任何其他东西允许任何随机单词和字符。

tables = "oms.claim|oms.order"

middle = "request|requested"

end = "respond|responded|answer|answered"

第一个示例:(示例需要匹配)因为至少有一个单词来自 tablesmiddle结束.

"Hello could you please define my username for oms.claim table as it is requested by order department and needs to be answered immediately."

第二个示例:(示例不需要匹配)因为 end 字符串中没有任何单词。

"Hi, oms.order table requested from me, could you please help me about that?"

我尝试过但失败了:

@bolt_app.message(re.compile("({tables}) ({middle} |(:?) {end})".
                         format(tables=config.kvkk_tables,middle=config.middle_sentence,end=config.end_of_sentence),re.IGNORECASE))

终于想通了

re.compile("{tables}.* {middles}.* {end}".format(tables=config.kvkk_tables, middles=config.middles, end=config.end_of_sentence),re.IGNORECASE)

所以关键点是使用 .*

This site 对我测试 python regex

帮助很大

注意:我还在 config.py 中用括号覆盖了我的字符串定义,如下所示:

tables = "(oms.claim|oms.order)"

middle = "(request|requested)"

end = "(respond|responded|answer|answered)"