如何修剪子字符串与正则表达式匹配的结果?
我在 postgresql 中有这组数据:
en_MY_V2_CID_MY_SEM_1_1_0_0
en_MY_V2_CID_MY_SEM_2_101_1703_0
en_MY_V2_CID_MY_SEM_2_101_1724_0
en_MY_CID_MY_DIS_1_100_1705_0068
en_MY_CID_MY_DIS_1_100_1705_0068
我想提取如下子字符串:
en_MY_V2_CID_MY_SEM_1
en_MY_V2_CID_MY_SEM_2
en_MY_V2_CID_MY_SEM_2
en_MY_CID_MY_DIS_1
en_MY_CID_MY_DIS_1
问题是,左侧结构不是恒定的,所以我无法使用正则表达式掌握它们。相反,右侧结构是常量,格式为_numbers_numbers_numbers_numbers
。所以我尝试在 postgresql: 中使用此查询:
select substring(name from '_\d+_\d+_\d+$') from logs;
并得到:
1_0_0
101_1703_0
101_1724_0
100_1705_0068
1705_0068
所以问题是,如何否定或修剪前一个子字符串以便得到结果?
I have this set of data in postgresql:
en_MY_V2_CID_MY_SEM_1_1_0_0
en_MY_V2_CID_MY_SEM_2_101_1703_0
en_MY_V2_CID_MY_SEM_2_101_1724_0
en_MY_CID_MY_DIS_1_100_1705_0068
en_MY_CID_MY_DIS_1_100_1705_0068
I want to extract the substring like these:
en_MY_V2_CID_MY_SEM_1
en_MY_V2_CID_MY_SEM_2
en_MY_V2_CID_MY_SEM_2
en_MY_CID_MY_DIS_1
en_MY_CID_MY_DIS_1
Problem is, the left-side structure is not constant so I can't grasp them using regex. Instead, the right-side structure is constant, with the format _numbers_numbers_numbers_numbers
. So I try to do use this query in postgresql:
select substring(name from '_\d+_\d+_\d+
And get:
1_0_0
101_1703_0
101_1724_0
100_1705_0068
1705_0068
So the question is, how do I negate, or trim the previous substring so I will get my results?
) from logs;
And get:
So the question is, how do I negate, or trim the previous substring so I will get my results?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 rexep_replace
Use rexep_replace