IPv6的正则表达式(RegEx)与IPv4分开 [英] Regular Expression (RegEx) for IPv6 Separate from IPv4

查看:213
本文介绍了IPv6的正则表达式(RegEx)与IPv4分开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在标记为重复之前请先阅读



我无法创建或找到适用于全部 IPv6格式(我的测试用例如下)。我注意到每个人都指向的问题:与有效的IPv6地址匹配的正则表达式但是,它们都将IPv6与IPv4



要求:


  1. 我不想要它也验证IPv4值,我已经有一个单独的验证功能的IPv4。

  2. 我需要一个模式在 Coldfusion PL / SQL 中的模式。

  3. 因为我在 PL / SQL 的模式必须保持低于512个字符。 Oracle仅支持RegExp语言的一小部分。因此, ColdFusion 模式最终可能不同于 PL / SQL 模式,




  4. b

    这是我尝试的最新模式:

      ^(?>(? a-f0-9] {1,4})(α>(α1)){7} |(α! {α,β}(α1)(α>(α1)){0,6}:(β2)α) >(α1)){5}:|(α!(α:。* [a-f0-9]:){6, )[0-9] [0-9] {2} [0-5] | 2 [0-4] [0-9] } | [1-9]?[0-9])(?> \。(?4)){3}))$ 

    这是靠近 ColdFusion 但不是100%。它在 PL / SQL 中根本不起作用。



    测试结果 http://regex101.com/r/wI8cI0 粗体项目该模式在 ColdFusion 中不起作用:


    1. 匹配

    2. 匹配

    3. 匹配

    4. 匹配

    5. 匹配
    6. $ b
    7. 匹配(但是@Michael Hampton说这不应该匹配,因为它不是有效的IPv6地址,但其他人告诉我它是有效的,所以我不确定这个测试用例)

    8. 匹配( :: 实际上是一个有效的格式,感谢@Sander Steffann。)

    9. 匹配

    10. 不匹配

    11. 匹配

    12. 不匹配
    13. 无匹配
    14. $ b
    15. 匹配

    16. 无匹配

    17. 无匹配

    18. 无匹配

    19. 无匹配

    我得到了测试用例8-11: http://publib.boulder.ibm.com/infocenter/iseries/v5r3/ index.jsp?topic =%2Frzai2%2Frzai2ipv6addrformat.htm
    并且被告知:测试9和11用于IPv6地址前缀,而不是IPv6地址,因此不应该匹配。



    最终结果,我需要他们在这样的语句中工作:



    ColdFusion:

     < cfset IndexOfOccurrence1 = REFind(^(α>(α>([a-f0-9] {1,4} (α1)(α1)(α:(α:f0-9)(α>:| $)){8, α1)){0,6}):(α2)α)|(α>(α>)(α>(α1)){5} (α:* [a-f0-9]:){6,})(θ3)(θ>(θ1)(θ> 0:0] [0-9] | 1 [0-9] {2} | [1-9]?[0-9])(? > \。(?4)){3}))$,value [i])> 

    PL / SQL:

      if(REGEXP_LIKE(v,'^(α>(α>([a-f0-9] {1,4} |(α×(α> 1)){0,6}(α: )◎:(α2)α)|(α>(α>(α1)(γ>(α1)){5} -9]:){6,})(θ3)(θ>(φ1)){0,4} 0-5] | 2 [0-4] [0-9] | 1 [0-9] {2} | [1-9]?[0-9] ){3}))$','i'))then 


    解决方案>

    在此答案中,@nhahtdh的大量帮助 http://stackoverflow.com/a/21943960/3112803 我发现打破它是最好的解决方案。下面是如何在 PL / SQL 中执行此操作的示例,但是在其他语言中可以这样做。我将在 ColdFusion 中执行相同操作。对于 PL / SQL 模式需要保持在512个字符以下,因此分拆它的工作伟大,它很容易理解。

      if(
    / * IPv6 expanded * /
    REGEXP_LIKE(v,'\A [[:xdigit:]] {1,4}(:[[:xdigit:]] {1,4}){7} \z')
    / * IPv6速记* /
    OR(NOT REGEXP_LIKE(v,'\A(。*?[[:xdigit:]](:| \z)){8}')
    AND REGEXP_LIKE v,'\A([[:xdigit:]] {1,4}(:[[:xdigit:]] {1,4}){0,6})?::([[:xdigit:] ] {@}(:[[:xdigit:]] {1,4}){0,6})?\z'))
    / * IPv6点阵四边形符号,扩展* /
    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点阵符号,简写* /
    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


    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 below). I am aware of this question that everyone points to: Regular expression that matches valid IPv6 addresses However, They all combine IPv6 with IPv4 and/or do not work with all my test cases.

    Requirements:

    1. I do not want it to also validate IPv4 values, I already have a separate validation function for IPv4.
    2. I need a pattern that works in Coldfusion and a pattern that works in PL/SQL.
    3. Because I'm using it in PL/SQL the pattern for it must stay under 512 characters. And Oracle supports only a narrow part of RegExp language. So the ColdFusion pattern could end up being different than the PL/SQL pattern, that is fine, so long as they both work.
    4. End result doesn't have be one long RegEx, it can be split up.

    Here is the latest pattern I'm trying out:

    ^(?>(?>([a-f0-9]{1,4})(?>:(?1)){7}|(?!(?:.*[a-f0-9](?>:|$)){8,})((?1)(?>:(?1)){0,6})?::(?2)?)|(?>(?>(?1)(?>:(?1)){5}:|(?!(?:.*[a-f0-9]:){6,})(?3)?::(?>((?1)(?>:(?1)){0,4}):)?)?(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(?>\.(?4)){3}))$
    

    This comes close for ColdFusion but not 100%. It doesn't work at all in PL/SQL.

    Test Results http://regex101.com/r/wI8cI0 The bold items are the ones the pattern doesn't work for in ColdFusion:

    1. match
    2. match
    3. match
    4. match
    5. match
    6. match (but @Michael Hampton says this should not match because it's not a valid IPv6 address, but others have told me it is valid, so I'm not sure about this test case.)
    7. match (:: is actually a valid format, thanks @Sander Steffann.)
    8. match
    9. no match
    10. match
    11. no match
    12. no match
    13. no match
    14. match
    15. match
    16. no match
    17. no match
    18. no match
    19. no match

    I got test cases 8-11 from: http://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?topic=%2Frzai2%2Frzai2ipv6addrformat.htm And was told: Test 9 and 11 are for IPv6 address prefix, not an IPv6 address, so those should not be match.

    End result, I need them to work in statements like this:

    ColdFusion:

    <cfset IndexOfOccurrence1=REFind("^(?>(?>([a-f0-9]{1,4})(?>:(?1)){7}|(?!(?:.*[a-f0-9](?>:|$)){8,})((?1)(?>:(?1)){0,6})?::(?2)?)|(?>(?>(?1)(?>:(?1)){5}:|(?!(?:.*[a-f0-9]:){6,})(?3)?::(?>((?1)(?>:(?1)){0,4}):)?)?(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(?>\.(?4)){3}))$",value[i])>
    

    PL/SQL:

    if ( REGEXP_LIKE(v,'^(?>(?>([a-f0-9]{1,4})(?>:(?1)){7}|(?!(?:.*[a-f0-9](?>:|$)){8,})((?1)(?>:(?1)){0,6})?::(?2)?)|(?>(?>(?1)(?>:(?1)){5}:|(?!(?:.*[a-f0-9]:){6,})(?3)?::(?>((?1)(?>:(?1)){0,4}):)?)?(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(?>\.(?4)){3}))$','i') ) then
    

    解决方案

    With much help from @nhahtdh in this answer http://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
    

    这篇关于IPv6的正则表达式(RegEx)与IPv4分开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆