在SQL Server 2008中通过regex删除特定文本的特定字符(Remove specific characters for a specific text via regex in SQL

编程入门 行业动态 更新时间:2024-10-24 12:29:41
在SQL Server 2008中通过regex删除特定文本的特定字符(Remove specific characters for a specific text via regex in SQL Server 2008)

我正在使用SQL Server 2008。

数据:

AA00012345/99 AX0000045687044/78 XB000077008/12

我需要做的是使用REGEX从输出中的字符串开始删除连续的零 (在看到0除外的数字后,允许连续的零)和斜杠(/)字符

预期产出:

AA1234599 AX4568704478 XB7700812

我设法通过这个查询来完成它:

select dbo.RegexGroup(t.Value,'(?<prefix>\w\w)(?<zeros>0*)(?<beforeslash>\d*)/(?<afterslash>\d\d)', 'prefix') + dbo.RegexGroup(t.Value,'(?<prefix>\w\w)(?<zeros>0*)(?<beforeslash>\d*)/(?<afterslash>\d\d)', 'beforeslash') + dbo.RegexGroup(t.Value,'(?<prefix>\w\w)(?<zeros>0*)(?<beforeslash>\d*)/(?<afterslash>\d\d)', 'afterslash') from table t

不过,我相信应该有更好更专业的方式来处理这个问题。 正则表达式的使用是必须的。 任何意见,将不胜感激。

I am using SQL Server 2008.

Data:

AA00012345/99 AX0000045687044/78 XB000077008/12

What I need to do is to remove consecutive zeros at the beginning only (after a number except 0 is seen, consecutive zeros are allowed) and the slash (/) chars from the string in the output by using REGEX.

Expected output:

AA1234599 AX4568704478 XB7700812

I managed to do it via this query:

select dbo.RegexGroup(t.Value,'(?<prefix>\w\w)(?<zeros>0*)(?<beforeslash>\d*)/(?<afterslash>\d\d)', 'prefix') + dbo.RegexGroup(t.Value,'(?<prefix>\w\w)(?<zeros>0*)(?<beforeslash>\d*)/(?<afterslash>\d\d)', 'beforeslash') + dbo.RegexGroup(t.Value,'(?<prefix>\w\w)(?<zeros>0*)(?<beforeslash>\d*)/(?<afterslash>\d\d)', 'afterslash') from table t

However, I believe there should be a better and more professional way to handle this issue. Regex usage is a must. Any advice would be appreciated.

最满意答案

这里有一个选项。 查询将字符串分为两部分,第一个字母,第二个休息部分,以数字开头。 通过替换空格并应用ltrim函数来删除0

declare @t table (c varchar(100)) insert into @t values ('AA00012345/99') ,('AX0000045687044/78') ,('XB000077008/12') select t.c, q2.p1 + replace(replace(ltrim(replace(q2.p2, '0', ' ')), ' ', '0'), '/', '') from @t t cross apply (select ci = patindex('%[0-9]%', t.c)) q1 cross apply (select p1 = substring(t.c, 1, q1.ci - 1), p2 = substring(t.c, q1.ci, len(t.c))) q2

输出:

AA00012345/99 AA12345/99 AX0000045687044/78 AX45687044/78 XB000077008/12 XB77008/12

Here's one option. Query divides strings into two parts, first - letters, second - rest part, starting with number. 0 is removed by replacing with space and applying ltrim function

declare @t table (c varchar(100)) insert into @t values ('AA00012345/99') ,('AX0000045687044/78') ,('XB000077008/12') select t.c, q2.p1 + replace(replace(ltrim(replace(q2.p2, '0', ' ')), ' ', '0'), '/', '') from @t t cross apply (select ci = patindex('%[0-9]%', t.c)) q1 cross apply (select p1 = substring(t.c, 1, q1.ci - 1), p2 = substring(t.c, q1.ci, len(t.c))) q2

Output:

AA00012345/99 AA12345/99 AX0000045687044/78 AX45687044/78 XB000077008/12 XB77008/12

更多推荐

本文发布于:2023-08-03 09:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1385273.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符   文本   regex   SQL   Server

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!