[英]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.