Sql 从字符串中拆分特定的数字链
下面有一个称为数据的表格:Sql 从字符串中拆分特定的数字链,sql,regex,postgresql,regex-greedy,Sql,Regex,Postgresql,Regex Greedy,下面有一个称为数据的表格: row comments 1 Fortune favors https://something.aaa.org/show_screen.cgi?id=548545 the 23 bold 2 No man 87485 is id# 548522 an island 65654. 3 125 Better id NEWLINE #546654 late than 5875565 never. 4 555 Bet
row comments
1 Fortune favors https://something.aaa.org/show_screen.cgi?id=548545 the 23 bold
2 No man 87485 is id# 548522 an island 65654.
3 125 Better id NEWLINE #546654 late than 5875565 never.
4 555 Better id546654 late than 565 never
我使用了以下查询:
select row, substring(substring(comments::text, '((id|ID) [0-9]+)'), '[0-9]+') as id
from data
where comments::text ~* 'id [0-9]+';
此查询输出忽略了第1到第3行。它刚刚处理了第4行:
row id
4 546654
你们中的一些人知道如何正确分割身份证号码吗?请注意,ID最多包含9位数字。使用regexp\u替换:
SELECT c.rownr
, regexp_replace (c.comments, e'.*[Ii][Dd][^0-9]*([0-9]+).*', '\1' ) AS the_id
, c.comments AS comments
FROM comments c
;
*匹配初始垃圾
`[Ii][Dd]匹配Id字符串,大小写不重要
[^0-9]*使用所有非数字字符
[0-9]+匹配所需的数字字符串
*匹配任何尾随字符
第3个参数中的“\1”表示希望第一个参数中的内容匹配
结果:
rownr | the_id | comments
-------+--------+--------------------------------------------------------------------------------
1 | 548545 | Fortune favors https://something.aaa.org/show_screen.cgi?id=548545 the 23 bold
2 | 548522 | No man 87485 is id# 548522 an island 65654.
3 | 546654 | 125 Better id NEWLINE #546654 late than 5875565 never.
4 | 546654 | 555 Better id546654 late than 565 never
(4 rows)