IPv6正则表达式(RegEx)在PL / SQL中不起作用

时间:2014-02-21 17:12:53

标签: regex oracle plsql ipv6

我似乎无法找出为什么这个正则表达式在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}))$/iD') ) then

用于验证IPv4和IPv6,它来自这里:https://stackoverflow.com/a/1934546/3112803

不确定这是否与此有关但我也在最后询问了关于D标志的问题:What Does This Regular Expression (RegEx) Flag Mean /iD

由于某些原因,此正则表达式适用于此网站上我的测试的 http://regex101.com/但在PL/SQL中,所有内容都无效。< / p>

的含义是,在某些情况下,我发现它失败了,但我一直在寻找几天,这是我能做的最好的发现不到512个字符(512是在REGEXP_LIKE中使用PL/SQL时的限制)

我很感激任何帮助。谢谢!

这些是我正在使用的测试用例...

{1: Initial address, regex should say valid/match}
select isValid('2001:0db8:0000:0000:0000:ff00:0042:8329','ipv6') from dual;

{2: After removing all leading zeroes, regex should say valid/match}
select isValid('2001:db8:0:0:0:ff00:42:8329','ipv6') from dual;

{3: After omitting consecutive sections of zeroes, regex should say valid/match}
select isValid('2001:db8::ff00:42:8329','ipv6') from dual;

{4: The loopback address, regex should say valid/match}
select isValid('0000:0000:0000:0000:0000:0000:0000:0001','ipv6') from dual;

{5: The loopback address be abbreviated to ::1 by using both rules, regex should say valid/match}
select isValid('::1','ipv6') from dual;

{6: This should be valid/match}
select isValid('ABCD:ABCD:ABCD:ABCD:ABCD:ABCD:192.168.158.190','ipv6') from dual;

{7: This should be valid/match}
select isValid('::','ipv6') from dual;

{8: IPv6 applications to communicate directly with IPv4 applications, regex should say valid/match}
select isValid('0:0:0:0:0:ffff:192.1.56.10','ipv6') from dual;

{9: should NOT be valid/match}
select isValid('::ffff:192.1.56.10/96','ipv6') from dual;

{old formats used for tunneling, these should NOT be valid/matches}
{10}
select isValid('0:0:0:0:0:0:192.1.56.10','ipv6') from dual;
{11}
select isValid('::192.1.56.10/96','ipv6') from dual;

{These 4 should be valid/match}
{12}
select isValid('::FFFF:129.144.52.38','ipv6') from dual;
{13}
select isValid('::129.144.52.38','ipv6') from dual;
{14}
select isValid('::FFFF:d','ipv6') from dual;
{15}
select isValid('1080:0:0:0:8:800:200C:417A','ipv6') from dual;

{These 4 should NOT be valid/match}
{16}
select isValid('::FFFF:d.d.d','ipv6') from dual;
{17}
select isValid('::FFFF:d.d','ipv6') from dual;
{18}
select isValid('::d.d.d','ipv6') from dual;
{19}
select isValid('::d.d','ipv6') from dual;

我被告知测试#6错了,ABCD:ABCD:ABCD:ABCD:ABCD:ABCD:192.168.158.190不是有效的IPv6地址,这是正确的吗?

测试用例8-11来自这里:http://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?topic=%2Frzai2%2Frzai2ipv6addrformat.htm但我被告知10&amp; 11不再使用。

3 个答案:

答案 0 :(得分:5)

不要在单个正则表达式中执行所有操作,最好将正则表达式分解为较小的正则表达式并对其进行测试:

if (
    /* IPv6 expanded */
    REGEX_LIKE(v, '\A[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7}\z', 'i')
    /* IPv6 shorthand */
    OR (NOT REGEX_LIKE(v, '\A(.*?[a-f0-9](:|\z)){8}', 'i')
        AND REGEX_LIKE(v, '\A([a-f0-9]{1,4}(:[a-f0-9]{1,4}){0,6})?::([a-f0-9]{1,4}(:[a-f0-9]{1,4}){0,6})?\z', 'i'))
    /* IPv6 dotted-quad notation, expanded */
    OR REGEX_LIKE(v, '\A[a-f0-9]{1,4}(:[a-f0-9]{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', 'i')
    /* IPv6 dotted-quad notation, shorthand */
    OR (NOT REGEX_LIKE(v, '\A(.*?[a-f0-9]:){6}', 'i')
        AND REGEX_LIKE(v, '\A([a-f0-9]{1,4}(:[a-f0-9]{1,4}){0,4})?::([a-f0-9]{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', 'i'))
   ) then

这只测试IPv6。不允许使用IPv4。

由于PL / SQL风格没有子程序调用{​​{1}},因此除了扩展所有内容外别无选择。缺乏负面预测(?n)迫使我们通过2次正则表达式测试操作来模拟它。

(?!pattern)\A用于匹配字符串的开头和结尾,因为它们都不受标志的影响,\z行为与{{1}相同在PCRE的\z模式下。

答案 1 :(得分:3)

你必须在开头摆脱/和最后的/ iD这是perl语法的一部分,表明它是一个正则表达式。

最后的i开关意味着忽略大小写并且可以作为regexp_like的额外参数给出,所以:

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') ) the

还有更多的问题,因为perl正则表达式不是100%等同于oracle正则表达式,我看到这里使用的模式不可用,如?&gt; 也许你可以拆分ipv4和ipv6之间的正则表达式,以避免在oracle中达到极限。只做REGEXP_LIKE(ip,'ipv4pattern')或REGEXP_LIKE(ip,'ipv6pattern')

将上述正则表达式的ipv4部分调整为在oracle中运行的东西给了我:

REGEXP_LIKE(ip,'^((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])$','i')

答案 2 :(得分:1)

REGEXP_LIKE(ip,'^(([\dA-F]{1,4}:([\dA-F]{1,4}:([\dA-F]{1,4}:([\dA-F]{1,4}:([\dA-F]{1,4}:[\dA-F]{0,4}|:[\dA-F]{1,4})?|(:[\dA-F]{1,4}){0,2})|(:[\dA-F]{1,4}){0,3})|(:[\dA-F]{1,4}){0,4})|:(:[\dA-F]{1,4}){0,5})((:[\dA-F]{1,4}){2}|:(25[0-5]|(2[0-4]|1\d|[1-9])?\d)(\.(25[0-5]|(2[0-4]|1\d|[1-9])?\d)){3})|(([\dA-F]{1,4}:){1,6}|:):[\dA-F]{0,4}|([\dA-F]{1,4}:){7}:)\z', 'i')

http://home.deds.nl/~aeron/regex/

的XML正则表中修改