Search code examples
pythonregexstringslack

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


Here is my example:

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

One of tables, middle and end string must be in sentence. When this condition matched we do not want to check any other things allow any random words and characters.

tables = "oms.claim|oms.order"

middle = "request|requested"

end = "respond|responded|answer|answered"

First Example:(Example needs to be matched) since there are at least 1 word from tables, middle and end.

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

Second Example:(Example needs to be NOT matched) since there are not any word from end string.

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

What I tried and failed:

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

Solution

  • Finally, I figured it out.

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

    So key point was using .*

    This site is helped me a lot to test python regex

    Note: I also covered my string definition in config.py with paranthesis like below:

    tables = "(oms.claim|oms.order)"
    
    middle = "(request|requested)"
    
    end = "(respond|responded|answer|answered)"