oracle中的正则表达式获取从100.64.*.*到100.127.*.*的IP地址范围?
regular expression in oracle to get a range of ip address from 100.64.*.* to 100.127.*.*?
我想获取从 100.64.. 到 100.127..
的 IP 地址范围
如果我有 table 命名的 testIp 和名为 PRIVATE_IP_ADDR 的列,下面是存在的记录:
1) 100.63.22.55
2) 100.64.102.558
3) 100.123.22.12
4) 100.127.22.55
5) 100.128.221.55
6) 100.125.355.10
7) 100.64.102.254
要求:
1. 2nd part of ip should be from 64 to 127 range
2. 3rd part of ip should be from 0 to 255 range
3. 4th part of ip should be from 0 to 255 range
正则表达式应该选择下面的 ip 地址
3) 100.123.22.12
4) 100.127.22.55
7) 100.64.102.254
到目前为止我尝试过的是:
select * from testip where REGEXP_LIKE (PRIVATE_IP_ADDR , '^(100\.
(6[4-9]|[7-9][0-9]|1[0-2][0-7])\.
([0-1]?[0-9]?[0-9]|2[0-5][0-5])\.
([0-1]?[0-9]?[0-9]|2[0-5][0-5]))' ) ;
but when i tried the result is
2) 100.64.102.558
3) 100.123.22.12
4) 100.127.22.55
7) 100.64.102.254
i dont want the record
2)100.64.102.558
IPv4 地址的正则表达式
当然对于 IPv4 地址,您可以使用正则表达式并只选择第二组数字。
这里我使用转义字符 \d
作为数字。正则表达式中的 2
只是表示我们想要一组数字的第二次出现。
最后,我将值转换为数字并使用 between 运算符。
SCOTT@db>list
1 WITH t AS
2 (SELECT '100.127.22.55' ip_address FROM dual
3 UNION ALL
4 SELECT '100.66.22.55' FROM dual
5 UNION ALL
6 SELECT '100.123.22.12' FROM dual
7 UNION ALL
8 SELECT '100.63.22.55' FROM dual
9 )
10 SELECT *
11 FROM
12 (SELECT t.ip_address,
13 regexp_substr(t.ip_address, '^\d+',1,1) first_number,
14 to_number(regexp_substr(t.ip_address, '\d+',1,2)) second_number
15 FROM t
16 )
17 WHERE 1=1
18 AND first_number = '100'
19* AND second_number BETWEEN 66 AND 127
SCOTT@db>/
IP_ADDRESS FIRST_NUMBER SECOND_NUMBER
------------- ---------------------------------------------------- -------------
100.123.22.12 100 123
100.127.22.55 100 127
100.66.22.55 100 66
根据操作评论更新
采用 regexp_like 方法(并考虑如何更广泛地描述问题),解决方案如下。使用量词时需要非常小心,我发现使用锚点很有帮助:
SCOTT@db>WITH t AS
2 (SELECT '100.63.22.55' ip_address FROM dual
3 UNION ALL
4 SELECT '100.64.102.558' ip_address FROM dual
5 UNION ALL
6 SELECT '100.123.22.12' ip_address FROM dual
7 UNION ALL
8 SELECT '100.127.22.55' ip_address FROM dual
9 UNION ALL
10 SELECT '100.128.221.55' ip_address FROM dual
11 UNION ALL
12 SELECT '100.125.355.10' ip_address FROM dual
13 UNION ALL
14 SELECT '100.64.102.254' ip_address FROM dual
15 )
16 SELECT *
17 FROM t
18 WHERE REGEXP_LIKE (t.ip_address , '^100\.(6[4-9]|[7-9][0-9]|1[0-2][0-7])(\.([1-9][0-9]?|1[0-9][0-9]|2([0-4][0-9]|5[0-5]))){2}$')
19 /
IP_ADDRESS
--------------
100.123.22.12
100.127.22.55
100.64.102.254
我想获取从 100.64.. 到 100.127..
的 IP 地址范围如果我有 table 命名的 testIp 和名为 PRIVATE_IP_ADDR 的列,下面是存在的记录:
1) 100.63.22.55
2) 100.64.102.558
3) 100.123.22.12
4) 100.127.22.55
5) 100.128.221.55
6) 100.125.355.10
7) 100.64.102.254
要求:
1. 2nd part of ip should be from 64 to 127 range
2. 3rd part of ip should be from 0 to 255 range
3. 4th part of ip should be from 0 to 255 range
正则表达式应该选择下面的 ip 地址
3) 100.123.22.12
4) 100.127.22.55
7) 100.64.102.254
到目前为止我尝试过的是:
select * from testip where REGEXP_LIKE (PRIVATE_IP_ADDR , '^(100\.
(6[4-9]|[7-9][0-9]|1[0-2][0-7])\.
([0-1]?[0-9]?[0-9]|2[0-5][0-5])\.
([0-1]?[0-9]?[0-9]|2[0-5][0-5]))' ) ;
but when i tried the result is
2) 100.64.102.558
3) 100.123.22.12
4) 100.127.22.55
7) 100.64.102.254
i dont want the record
2)100.64.102.558
IPv4 地址的正则表达式
当然对于 IPv4 地址,您可以使用正则表达式并只选择第二组数字。
这里我使用转义字符 \d
作为数字。正则表达式中的 2
只是表示我们想要一组数字的第二次出现。
最后,我将值转换为数字并使用 between 运算符。
SCOTT@db>list
1 WITH t AS
2 (SELECT '100.127.22.55' ip_address FROM dual
3 UNION ALL
4 SELECT '100.66.22.55' FROM dual
5 UNION ALL
6 SELECT '100.123.22.12' FROM dual
7 UNION ALL
8 SELECT '100.63.22.55' FROM dual
9 )
10 SELECT *
11 FROM
12 (SELECT t.ip_address,
13 regexp_substr(t.ip_address, '^\d+',1,1) first_number,
14 to_number(regexp_substr(t.ip_address, '\d+',1,2)) second_number
15 FROM t
16 )
17 WHERE 1=1
18 AND first_number = '100'
19* AND second_number BETWEEN 66 AND 127
SCOTT@db>/
IP_ADDRESS FIRST_NUMBER SECOND_NUMBER
------------- ---------------------------------------------------- -------------
100.123.22.12 100 123
100.127.22.55 100 127
100.66.22.55 100 66
根据操作评论更新
采用 regexp_like 方法(并考虑如何更广泛地描述问题),解决方案如下。使用量词时需要非常小心,我发现使用锚点很有帮助:
SCOTT@db>WITH t AS
2 (SELECT '100.63.22.55' ip_address FROM dual
3 UNION ALL
4 SELECT '100.64.102.558' ip_address FROM dual
5 UNION ALL
6 SELECT '100.123.22.12' ip_address FROM dual
7 UNION ALL
8 SELECT '100.127.22.55' ip_address FROM dual
9 UNION ALL
10 SELECT '100.128.221.55' ip_address FROM dual
11 UNION ALL
12 SELECT '100.125.355.10' ip_address FROM dual
13 UNION ALL
14 SELECT '100.64.102.254' ip_address FROM dual
15 )
16 SELECT *
17 FROM t
18 WHERE REGEXP_LIKE (t.ip_address , '^100\.(6[4-9]|[7-9][0-9]|1[0-2][0-7])(\.([1-9][0-9]?|1[0-9][0-9]|2([0-4][0-9]|5[0-5]))){2}$')
19 /
IP_ADDRESS
--------------
100.123.22.12
100.127.22.55
100.64.102.254