Regex using negative lookahead is not working properly

Viewed 104

Regex pattern:

^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-].(?!.*\.TEST).*@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*(\.COM|\.EDU)+$

I want my pattern to match an email that does NOT include ".TEST" (using negative lookahead) right before "@" and ends with "COM" or "EDU".

This pattern works for the most part (so far), but incorrectly matches "(" when entered in the 1st half of the pattern. I realize that it's because I have "." instead of "+" after the first bit, but if I don't, then everything else doesn't work.

I've looked around, but could not find a similar enough solution. I'm bad at regex.

Any help would be greatly appreciated.

1 Answers

Use

^(?!.*\.TEST@)[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*\.(?:COM|EDU)$

See proof.

Explanation

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
    \.                       '.'
--------------------------------------------------------------------------------
    TEST@                    'TEST@'
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  [a-zA-Z0-                 any character of: 'a' to 'z', 'A' to 'Z',
  9.!#$%&'*+/=?^_`{|}~-     '0' to '9', '.', '!', '#', '$', '%', '&',
  ]+                       ''', '*', '+', '/', '=', '?', '^', '_',
                           '`', '{', '|', '}', '~', '-' (1 or more
                           times (matching the most amount possible))
--------------------------------------------------------------------------------
  @                        '@'
--------------------------------------------------------------------------------
  [a-zA-Z0-9-]+            any character of: 'a' to 'z', 'A' to 'Z',
                           '0' to '9', '-' (1 or more times (matching
                           the most amount possible))
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (0 or more times
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    \.                       '.'
--------------------------------------------------------------------------------
    [a-zA-Z0-9-]+            any character of: 'a' to 'z', 'A' to
                             'Z', '0' to '9', '-' (1 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
  )*                       end of grouping
--------------------------------------------------------------------------------
  \.                       '.'
--------------------------------------------------------------------------------
  (?:                      group, but do not capture:
--------------------------------------------------------------------------------
    COM                      'COM'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    EDU                      'EDU'
--------------------------------------------------------------------------------
  )                        end of grouping
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
Related