使用负前瞻的正则表达式无法正常工作

Regex using negative lookahead is not working properly

正则表达式模式:

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

我希望我的模式匹配在“@”之前不包含“.TEST”(使用否定前瞻)并以“COM”或“EDU”结尾的电子邮件。

此模式在大部分情况下(到目前为止)都有效,但在模式的第一半部分输入时错误匹配“(”。我意识到这是因为我在模式后有“.”而不是“+”第一点,但如果我不这样做,那么其他一切都不起作用。

我环顾四周,但找不到足够相似的解决方案。我不擅长正则表达式。

如有任何帮助,我们将不胜感激。

使用

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

参见proof

说明

--------------------------------------------------------------------------------
  ^                        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