简体   繁体   English

使用 sql 查找模式的正则表达式

[英]Regex to find a pattern using sql

I have a column called 'user_details' in a SQL table 'customers' with the below value in it:我在 SQL 表“客户”中有一个名为“用户详细信息”的列,其中包含以下值:

{4:"2021-06-07T16:17:26.327+02:00",5:"1623075805735.phna3uyo",6:"www.abc.com/connexion",10:"loggedOut",12:"567879",2:"1026530505.1619610156",3:"event"}
{4:"2021-06-01T13:11:34.742+02:00",5:"1622545894742.ml2cyuw",6:"www.seigneuriegauthier.com/connexion",10:"loggedOut",12:"",2:"435305774.1622545085",3:"event"}
{4:"2021-06-01T10:13:30.85+02:00",5:"1622535210085.vlowlxmj",6:"www.seigneuriegauthier.com/connexion",10:"loggedOut",12:"278356",2:"1381684281.1622534907",3:"event"}
{4:"2021-06-01T10:24:51.808+02:00",5:"1622536405142.h45exkgg",6:"seigneuriegauthier.com/connexion",10:"loggedOut",12:"251666",2:"1019448131.1621925108",3:"event"}
{4:"2021-06-01T14:13:54.476+02:00",5:"1622551449049.k14838ij",6:"www.seigneuriegauthier.com/connexion",10:"loggedOut",12:"601322",2:"1975087820.1622548509",3:"event"}

I'm trying to extract the id after number 12:" without the "" ie 567879,278356 etc into another column.我正在尝试将数字 12:" 之后的 id 提取到另一列中,而没有 "" 即 567879,278356 等。

Since there are many reputative "" I'm unable to build the regex expression.由于有许多著名的“”,我无法构建正则表达式。 I tried the below but didn't get the exact match我尝试了以下但没有得到完全匹配

(?<=:)"[0-9][0-9][0-9][0-9][0-9][0-9]

How can I write a SQL query to retrieve this.如何编写 SQL 查询来检索它。 Pls help.请帮忙。

On SQL Server, with no regex, support, we can try using the base string functions SUBSTRING along with CHARINDEX :在 SQL 服务器上,不支持正则表达式,我们可以尝试使用基本字符串函数SUBSTRINGCHARINDEX

SELECT
    user_details,
    SUBSTRING(user_details,
              CHARINDEX('12:"', user_details) + 4,
              CHARINDEX('"', user_details, CHARINDEX('12:"', user_details) + 4) -
                  CHARINDEX('12:"', user_details) - 4) AS val
FROM customers;

Demo 演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM