Search code examples
pythonregexletters

regex: find all words with certain letters but not other


can anyone is help me with that:

I need to find all words from list containing letters [t OR d] AND [k OR c] but not any of [s,z,n,m]

I figured out first part, but don't know how to include stop list:

\w*[t|d]\w*[k|c]\w*

in Python notation

Thank you in advance


Solution

  • Based on answer of Padraic

    EDIT We both missed this condition

    [t OR d] AND [k OR c]

    So - fixed accordingly

    s = "detected dot knight track"
    
    allowed = ({"t","d"},{"k","c"})
    forbidden = {"s","z","n", "m"}
    
    for word in s.split():
        letter_set = set(word)
        if all(letter_set & a for a in allowed) and letter_set - forbidden == letter_set:
            print(word)
    

    And the result is

    detected
    track