[英]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"
第一个示例:(示例需要匹配)因为至少有一个单词来自tables 、 middle和end 。
“您好,请您为 oms.claim 表定义我的用户名,因为它是订单部门要求的,需要立即答复。”
第二个示例:(示例不需要匹配)因为结尾字符串中没有任何单词。
“嗨,oms.order 表向我请求,你能帮我解决这个问题吗?”
我尝试过但失败了:
@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)
所以关键点是使用.*
这个网站对我测试 python 正则表达式帮助很大
注意:我还在 config.py 中用括号覆盖了我的字符串定义,如下所示:
tables = "(oms.claim|oms.order)"
middle = "(request|requested)"
end = "(respond|responded|answer|answered)"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.