假设我有一个类似的地址:
model<-ksvm(STT~.,data=prediction1_train,kernel="linear",cost=10,scale=FALSE)
我希望输出像
2001:04E8:0000:4001:0000:0000:0000:0000/64
所以基本上,它会删除前导零,同时用一个零替换四个零。
2001:4E8:0:4001:0:0:0:0/64
上面的正则表达式完成了这项工作,但输出为
replaceAll("(:(0)*)|(^0+)",":")
这四个零都被省略了。
注意:输入可能是任何有效的ipv6地址
答案 0 :(得分:2)
您可以使用REPLACE
两次,SUBSTR
删除前导0。
SELECT ipv6,
SUBSTR(REPLACE(REPLACE(':' || ipv6, ':00', ':'),':0', ':'),2) AS result
FROM tab;
的 SqlFiddleDemo
强>
演示正在使用MySQL
,因为Oracle
没有响应。唯一的变化是字符串连接。
输出:
╔═════════════════════════════════════════════╦════════════════════════════╗
║ ipv6 ║ result ║
╠═════════════════════════════════════════════╬════════════════════════════╣
║ 2001:04E8:0000:4001:0000:0000:0000:0000/64 ║ 2001:4E8:0:4001:0:0:0:0/64 ║
║ 0000:0001:0012:0123:1234:0000:0000:0000/64 ║ 0:1:12:123:1234:0:0:0/64 ║
║ 0001:04E8:0000:4001:0000:0000:0000:0000/64 ║ 1:4E8:0:4001:0:0:0:0/64 ║
║ 0012:04E8:0000:4001:0000:0000:0000:0000/64 ║ 12:4E8:0:4001:0:0:0:0/64 ║
║ 0123:04E8:0000:4001:0000:0000:0000:0000/64 ║ 123:4E8:0:4001:0:0:0:0/64 ║
╚═════════════════════════════════════════════╩════════════════════════════╝
答案 1 :(得分:1)
使用Oracle的Regexp_Replace:
的示例 SQL> with t as (
2 select '2001:04E8:0000:4001:0000:0000:0000:0000/64' str from dual union all
3 select '0000:0001:0012:0123:1234:0000:0000:0000/64' from dual union all
4 select '0001:04E8:0000:4001:0000:0000:0000:0000/64' from dual union all
5 select '0000:0001:0012:0123:1234:0000:0000:0000/64' from dual
6 ) -- end of sample data
7 select regexp_replace(str, '(:|^)(0{0,3})([1-9]*)', '\1\3') str
8 from t;
STR
--------------------------------------------------------------------------------
2001:4E8:0:4001:0:0:0:0/64
0:1:12:123:1234:0:0:0/64
1:4E8:0:4001:0:0:0:0/64
0:1:12:123:1234:0:0:0/64
答案 2 :(得分:0)
在java中,类似这样的东西:
replaceAll("(:000)|(:0*)", ":");
答案 3 :(得分:0)
试试这个:
WITH t AS (
SELECT '2001:04E8:0000:4001:0000:0000:0000:0000/64' str FROM dual UNION ALL
SELECT '0000:0001:0012:0123:1234:0000:0000:0000/64' FROM dual UNION ALL
SELECT '0001:04E8:0000:4001:0000:0000:0000:0000/64' FROM dual UNION ALL
SELECT '0000:0001:0012:0123:1234:0000:0000:0000/64' FROM dual UNION ALL
SELECT '2001:04E8:0000:4001:0000:0:0000:0/64' FROM dual
)
SELECT
REGEXP_REPLACE(str, '(:|^)0+([[:xdigit:]]+)', '\1\2')
from t;
2001:4E8:0:4001:0:0:0:0/64
0:1:12:123:1234:0:0:0/64
1:4E8:0:4001:0:0:0:0/64
0:1:12:123:1234:0:0:0/64
2001:4E8:0:4001:0:0:0:0/64