Python Forum

Full Version: Regex using the AND NOT operator
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I'm looking for a regular expression that I find in the text, the words "ata de audiência" OR "termo de audiência", and find "inconcilia" AND "reclamante" AND "reclamada", and do NOT find the word "sentença"

    if(re.search((r'ata de audiência' or r'termo de audiência') and r'inconcilia' and r'reclamada' and r'reclamante' and not r'sentença', content.read()))):
                key_content = True
I tried to do it this way.. i can find the words, but when I use the "AND NOT" operator, the regular expression does not work

ps: there are words in portuguese
edit:I'm learning python so I'm probably taking the wrong approach
What you have, is just a bunch of strings and/or'd together, which just evaluates to True, and True isn't a regex, so it'll (probably) always fail.

I really like https://www.regexpal.com/ for putting in sample inputs that should match and fail, so I can test the regex as I write it.  In your case, since the words/phrases can appear in any order, I don't actually know offhand how to write a regex for that.  Does it HAVE to be a regex?  Because that'd be simple with just standard string searching...
text = content.read()
key_content = False
if "sentença" not in text:
    if any(phrase in text for phrase in ["ata de audiência", "termo de audiência"]):
        if all(word in text for word in ["inconcilia", "reclamada", "reclamante"])):
            key_content = True
Does the phrase need to be in this order?

ata de audiência inconcilia reclamada reclamante