Search code examples
regexgore2

RE2 or Go Regex to match all ccTLD emails except for .us or .co


How can I create a filter for all ccTLD email addresses except for the ones that end with .co or .us using the Go RE2 regexp syntax?

I am trying to send all emails originating from a ccTLD to our quarantine to avoid SPAM and malware, but we don't want to quarantine .co or .us addresses because they are not typically used by adversaries.

I've attempted to use the following regex pattern, but it is invalid RE2:

^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.(?!us|co)[a-zA-Z]{2,}$

Edit: I'm updating to clarify, but my question was downvoted without much to go on. But the Go Regex (or RE2) is configured to match on the sender header. I'm looking to match all ccTLDs such as spam@domain.de, or fake@email.in, except for .us and .co. The reason for this is because I have found a trend in all of the SPAM email that we receive where 95% of the SPAM comes from a ccTLD (I think because there are fewer restrictions for reporting abuse)

I don't care about what comes before the @, just after. This is a challenging question, I am aware of this fact, but please provide a basis if you downvote the question to help.


Solution

  • I found the answer thanks to another user's comment, which has since been deleted.

    /^.+@.+\.(u[a-rt-z]|c[a-np-z]|[abd-t-vz][a-z])$/gmi
    

    Matches:

    • bad@email.jp
    • List item
    • List item
    • really@bad.cn
    • not@trusted.ru
    • block@domain.com.jp
    • another@example.cn.cn
    • phake@email.ai
    • sure@bet-jp.jp

    Non-Matches:

    • first.l@some-st.com
    • known@good.com
    • test@gmail.com
    • also@good.co
    • good@email.us
    • heres@another.mil
    • No-Reply@ci.city.st.us