【问题标题】:Regex using negative lookahead is not working properly使用负前瞻的正则表达式无法正常工作
【发布时间】:2021-01-14 11:27:54
【问题描述】:

正则表达式模式:

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

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

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

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

任何帮助将不胜感激。

【问题讨论】:

    标签: javascript regex regex-lookarounds


    【解决方案1】:

    使用

    ^(?!.*\.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
    

    【讨论】:

    • 好像是这个,谢谢!我将不得不通过您的解释阅读更多内容,但问题是我没有把它放在前面?
    • @SlavictheSlavic @ 字符必须放在前瞻中以在@ 之前检查.TEST,是的,在表达式的开头放置这样的前瞻。
    • 啊好的。如果我想在前瞻中添加 OR 怎么办。似乎也很挑剔。
    • Nvmd 认为我明白了。它是 (?!.*\.ctr@|.*\.@)。已将您标记为正确答案。
    • @SlavictheSlavic 你也可以使用(?!.*\.(?:ctr)?@)
    猜你喜欢
    • 1970-01-01
    • 2011-10-14
    • 2010-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多