Regular [removed]RegEx) for IPv6 Separate from IPv4

前端 未结 3 1643
孤独总比滥情好
孤独总比滥情好 2020-12-07 00:16

Please read before marking as duplicate

I have not been able to create or find a RegEx that works for all IPv6 formats (my test cases are b

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-07 00:59

    With much help from @nhahtdh in this answer https://stackoverflow.com/a/21943960/3112803 I have found breaking it up to be the best solution. Below is an example of how to do it in PL/SQL, but it could be done this way in other languages. I'll do the same in ColdFusion. For PL/SQL the pattern needed to stay under 512 characters so breaking it up works great and it is simple to understand. It passed all my test cases in the original question.

    if (
        /* IPv6 expanded */
        REGEXP_LIKE(v, '\A[[:xdigit:]]{1,4}(:[[:xdigit:]]{1,4}){7}\z')
        /* IPv6 shorthand */
        OR (NOT REGEXP_LIKE(v, '\A(.*?[[:xdigit:]](:|\z)){8}')
        AND REGEXP_LIKE(v, '\A([[:xdigit:]]{1,4}(:[[:xdigit:]]{1,4}){0,6})?::([[:xdigit:]]{1,4}(:[[:xdigit:]]{1,4}){0,6})?\z'))
        /* IPv6 dotted-quad notation, expanded */
        OR REGEXP_LIKE(v, '\A[[:xdigit:]]{1,4}(:[[:xdigit:]]{1,4}){5}:(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}\z')
        /* IPv6 dotted-quad notation, shorthand */
        OR (NOT REGEXP_LIKE(v, '\A(.*?[[:xdigit:]]:){6}')
        AND REGEXP_LIKE(v, '\A([[:xdigit:]]{1,4}(:[[:xdigit:]]{1,4}){0,4})?::([[:xdigit:]]{1,4}:){0,5}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}\z'))
    ) then
    

提交回复
热议问题