【问题标题】:regex to validate comma seperated IPv4 and cidr正则表达式验证逗号分隔的 IPv4 和 cidr
【发布时间】:2021-08-26 12:10:52
【问题描述】:

我有这个验证 IPV4 和 IPV6 ip 地址和 CIDR 的正则表达式,但我只想验证 ipv4 地址和 cidr

^(((((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/([1-9]|1[0-9]|2[0-8]|3[0-2])){0,1}){0,1})|((((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]d|1dd|[1-9]?d)(.(25[0-5]|2[0-4]d|1dd|[1-9]?d)){3}))|:)))(%.+)?s*(\/([0-9]|[1-9][0-9]|1[0-1][0-9]|12[0-8])){0,1})))((\s*,\s*)(?=[^,])){0,1})+$

有人可以帮我解决这个问题吗?

【问题讨论】:

  • 10.0.0.0,192.0.0.0/24,172.0.0.0 这就是我想要的
  • 好的,我会帮你的。每个地方的上限是多少?
  • 最大?我没有得到你

标签: regex ip ipv4 cidr


【解决方案1】:

删除不必要的:

^(((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])(\/([1-9]|1[0-9]|2[0-8]|3[0-2])){0,1}){0,1}((\s*,\s*)(?=[^,])){0,1})+$

regex proof

解释

--------------------------------------------------------------------------------
  ^                        the beginning of the string
--------------------------------------------------------------------------------
  (                        group and capture to \1 (1 or more times
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    (                        group and capture to \2 (between 0 and 1
                             times (matching the most amount
                             possible)):
--------------------------------------------------------------------------------
      (                        group and capture to \3 (3 times):
--------------------------------------------------------------------------------
        (                        group and capture to \4:
--------------------------------------------------------------------------------
          [0-9]                    any character of: '0' to '9'
--------------------------------------------------------------------------------
         |                        OR
--------------------------------------------------------------------------------
          [1-9]                    any character of: '1' to '9'
--------------------------------------------------------------------------------
          [0-9]                    any character of: '0' to '9'
--------------------------------------------------------------------------------
         |                        OR
--------------------------------------------------------------------------------
          1                        '1'
--------------------------------------------------------------------------------
          [0-9]{2}                 any character of: '0' to '9' (2
                                   times)
--------------------------------------------------------------------------------
         |                        OR
--------------------------------------------------------------------------------
          2                        '2'
--------------------------------------------------------------------------------
          [0-4]                    any character of: '0' to '4'
--------------------------------------------------------------------------------
          [0-9]                    any character of: '0' to '9'
--------------------------------------------------------------------------------
         |                        OR
--------------------------------------------------------------------------------
          25                       '25'
--------------------------------------------------------------------------------
          [0-5]                    any character of: '0' to '5'
--------------------------------------------------------------------------------
        )                        end of \4
--------------------------------------------------------------------------------
        \.                       '.'
--------------------------------------------------------------------------------
      ){3}                     end of \3 (NOTE: because you are using
                               a quantifier on this capture, only the
                               LAST repetition of the captured
                               pattern will be stored in \3)
--------------------------------------------------------------------------------
      (                        group and capture to \5:
--------------------------------------------------------------------------------
        [0-9]                    any character of: '0' to '9'
--------------------------------------------------------------------------------
       |                        OR
--------------------------------------------------------------------------------
        [1-9]                    any character of: '1' to '9'
--------------------------------------------------------------------------------
        [0-9]                    any character of: '0' to '9'
--------------------------------------------------------------------------------
       |                        OR
--------------------------------------------------------------------------------
        1                        '1'
--------------------------------------------------------------------------------
        [0-9]{2}                 any character of: '0' to '9' (2
                                 times)
--------------------------------------------------------------------------------
       |                        OR
--------------------------------------------------------------------------------
        2                        '2'
--------------------------------------------------------------------------------
        [0-4]                    any character of: '0' to '4'
--------------------------------------------------------------------------------
        [0-9]                    any character of: '0' to '9'
--------------------------------------------------------------------------------
       |                        OR
--------------------------------------------------------------------------------
        25                       '25'
--------------------------------------------------------------------------------
        [0-5]                    any character of: '0' to '5'
--------------------------------------------------------------------------------
      )                        end of \5
--------------------------------------------------------------------------------
      (                        group and capture to \6 (between 0 and
                               1 times (matching the most amount
                               possible)):
--------------------------------------------------------------------------------
        \/                       '/'
--------------------------------------------------------------------------------
        (                        group and capture to \7:
--------------------------------------------------------------------------------
          [1-9]                    any character of: '1' to '9'
--------------------------------------------------------------------------------
         |                        OR
--------------------------------------------------------------------------------
          1                        '1'
--------------------------------------------------------------------------------
          [0-9]                    any character of: '0' to '9'
--------------------------------------------------------------------------------
         |                        OR
--------------------------------------------------------------------------------
          2                        '2'
--------------------------------------------------------------------------------
          [0-8]                    any character of: '0' to '8'
--------------------------------------------------------------------------------
         |                        OR
--------------------------------------------------------------------------------
          3                        '3'
--------------------------------------------------------------------------------
          [0-2]                    any character of: '0' to '2'
--------------------------------------------------------------------------------
        )                        end of \7
--------------------------------------------------------------------------------
      ){0,1}                   end of \6 (NOTE: because you are using
                               a quantifier on this capture, only the
                               LAST repetition of the captured
                               pattern will be stored in \6)
--------------------------------------------------------------------------------
    ){0,1}                   end of \2 (NOTE: because you are using a
                             quantifier on this capture, only the
                             LAST repetition of the captured pattern
                             will be stored in \2)
--------------------------------------------------------------------------------
    (                        group and capture to \8 (between 0 and 1
                             times (matching the most amount
                             possible)):
--------------------------------------------------------------------------------
      (                        group and capture to \9:
--------------------------------------------------------------------------------
        \s*                      whitespace (\n, \r, \t, \f, and " ")
                                 (0 or more times (matching the most
                                 amount possible))
--------------------------------------------------------------------------------
        ,                        ','
--------------------------------------------------------------------------------
        \s*                      whitespace (\n, \r, \t, \f, and " ")
                                 (0 or more times (matching the most
                                 amount possible))
--------------------------------------------------------------------------------
      )                        end of \9
--------------------------------------------------------------------------------
      (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
        [^,]                     any character except: ','
--------------------------------------------------------------------------------
      )                        end of look-ahead
--------------------------------------------------------------------------------
    ){0,1}                   end of \8 (NOTE: because you are using a
                             quantifier on this capture, only the
                             LAST repetition of the captured pattern
                             will be stored in \8)
--------------------------------------------------------------------------------
  )+                       end of \1 (NOTE: because you are using a
                           quantifier on this capture, only the LAST
                           repetition of the captured pattern will be
                           stored in \1)
--------------------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string

【讨论】:

  • 完美,谢谢
  • @RenuManhas 太好了,请接受解决方案,单击左侧的灰色标记,然后单击右侧的向上箭头进行投票。
最近更新 更多