Search code examples
asp.netregexweb-config

What's the breakdown of the following regex?


^((?!ca-ct.mydomain)(?!ca.mydomain)(?!cats.mydomain).)*mydomain.com$

I got the above expression from a web.config file, it's supposed to filter out anything that contains ca-ct.mydomain or ca.mydomain or cats.mydomain

I just cannot understand what the .)* piece means, the closing parenthesis between the dot and the asterisk seem to break the otherwise logical "any amount of characters after matching any of the 3 negative lookaheads" piece.


Solution

  • The negative look-ahead assertions are checked at successive positions. After consuming one character with ., a repetition with * will apply those assertions again at the next position, ...and so on.

    It is just one way to do it. Another approach is to have the three negative look-ahead assertions execute only once, only at the beginning of the input, and look further in the input (by incorporating .*). Then when these assertions succeed, the input can be consumed with yet another .*:

    ^(?!.*ca-ct.mydomain)(?!.*ca.mydomain)(?!.*cats.mydomain).*mydomain.com$
    

    The work involved for the regex engine is similar though.