字符串模式替换列

发布于 2024-12-15 12:40:36 字数 920 浏览 1 评论 0原文

由于在 SQL 中执行正则表达式并不容易,因此我需要一些关于如何解决此问题的建议。

我有一列包含以下类型的数据:

Lorem ipsum dolor sat $%foo##amet%$,consectetur adipiscing elit。 Nullam odio risus,mollis a interdum vitae,rutrum id leo。佩伦特斯克 达皮布斯·洛博蒂斯·马蒂斯。 Praesent at nisi a orci commodo celerisque $%bar##%$ eget id dui。 Morbi est arcu, ultricies et consequat ac, Pretium sed mi。 Quisque iaculis pretium congue。智人埃蒂亚姆 eu mauris tristique 位于 venenatis mauris ultricies。 Proin eu vehicula enim。 前庭 aliquam、mauris ac tempus vulputate、odio mauris rhoncus purus、 id suscipit velit Erat quis magna。

我需要匹配的粗体文本,并且需要将其替换为第二部分中找到的文本。

含义:

  • $%foo##amet%$ 变为 amet
  • $%bar##%$ 变为空字符串。

作为正则表达式的模式类似于 \$%[^#]+?##([^%]*?)%\$

但我不能真正使用它,因为正则表达式不是tsql 确实支持...

有什么建议吗?

Since it's not that easy to perform regex in SQL I'll need some advice in how to solve this problem.

I have a column with the following type of data:

Lorem ipsum dolor sit $%foo##amet%$, consectetur adipiscing elit.
Nullam odio risus, mollis a interdum vitae, rutrum id leo. Pellentesque
dapibus lobortis mattis. Praesent at nisi a orci commodo scelerisque
$%bar##%$ eget id dui. Morbi est arcu, ultricies et consequat ac,
pretium sed mi. Quisque iaculis pretium congue. Etiam ullamcorper sapien
eu mauris tristique at venenatis mauris ultricies. Proin eu vehicula enim.
Vestibulum aliquam, mauris ac tempus vulputate, odio mauris rhoncus purus,
id suscipit velit erat quis magna.

The bold text I need to match, and it needs to be replaced with the text found in the second part.

Meaning:

  • $%foo##amet%$ becomes amet
  • $%bar##%$ becomes an empty string.

The pattern as a regex would be something like \$%[^#]+?##([^%]*?)%\$

I can't really use that though since regex is not really supported in tsql...

Any advice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南城旧梦 2024-12-22 12:40:36

最好的选择是使用带有固定匹配字符串的嵌套 REPLACE

SELECT REPLACE(
           REPLACE(YourColumn, '$[%]foo##amet[%]

当然,这不具备正则表达式的灵活性......

或者您可以设计一个 SQL-CLR 正则表达式库(或查找预先存在的内容之一)并将其包含到您的 SQL 中服务器 - 从 2005 年开始,SQL Server可以执行 .NET 代码,而正则表达式库之类的东西是使用 .NET 功能扩展 T-SQL 的完美示例。

, 'amet'), '$[%]bar##[%]

当然,这不具备正则表达式的灵活性......

或者您可以设计一个 SQL-CLR 正则表达式库(或查找预先存在的内容之一)并将其包含到您的 SQL 中服务器 - 从 2005 年开始,SQL Server可以执行 .NET 代码,而正则表达式库之类的东西是使用 .NET 功能扩展 T-SQL 的完美示例。

, '')

当然,这不具备正则表达式的灵活性......

或者您可以设计一个 SQL-CLR 正则表达式库(或查找预先存在的内容之一)并将其包含到您的 SQL 中服务器 - 从 2005 年开始,SQL Server可以执行 .NET 代码,而正则表达式库之类的东西是使用 .NET 功能扩展 T-SQL 的完美示例。

The best option you have is using a nested REPLACE with fixed matching string:

SELECT REPLACE(
           REPLACE(YourColumn, '$[%]foo##amet[%]

Of course, that doesn't have the flexibility of regexes....

Or you could design a SQL-CLR regex library (or find one of the pre-existing ones) and include those into your SQL Server - from 2005 on, SQL Server can execute .NET code, and something like a regex library is a perfect example for extending T-SQL with .NET capabilities.

, 'amet'), '$[%]bar##[%]

Of course, that doesn't have the flexibility of regexes....

Or you could design a SQL-CLR regex library (or find one of the pre-existing ones) and include those into your SQL Server - from 2005 on, SQL Server can execute .NET code, and something like a regex library is a perfect example for extending T-SQL with .NET capabilities.

, '')

Of course, that doesn't have the flexibility of regexes....

Or you could design a SQL-CLR regex library (or find one of the pre-existing ones) and include those into your SQL Server - from 2005 on, SQL Server can execute .NET code, and something like a regex library is a perfect example for extending T-SQL with .NET capabilities.

埋葬我深情 2024-12-22 12:40:36

您必须执行以下代码:

SELECT REPLACE(REPLACE(YourColumn, ' $%amet%$ ', ' amet '), ' $%bar%$ ', ' ')

% 字符代表“任何零个或多个字符的字符串”,如下所述 此处
您需要添加空格来仅识别单个单词:)

You have to do the following code:

SELECT REPLACE(REPLACE(YourColumn, ' $%amet%$ ', ' amet '), ' $%bar%$ ', ' ')

The % character represents "Any string of zero or more characters" as explained here.
You need to put spaces to identify only single words :)

会发光的星星闪亮亮i 2024-12-22 12:40:36

用这个函数修复它:

CREATE FUNCTION [dbo].[ReplaceWithDefault]
(
   @InputString VARCHAR(4000)
)
RETURNS VARCHAR(4000)
AS
BEGIN
    DECLARE @Pattern VARCHAR(100) SET @Pattern = '$[%]_%##%[%]

    -- working copy of the string
    DECLARE @Result VARCHAR(4000) SET @Result = @InputString
    -- current match of the pattern
    DECLARE @CurMatch VARCHAR(500) SET @curMatch = ''
    -- string to replace the current match
    DECLARE @Replace VARCHAR(500) SET @Replace = ''
    -- start + end of the current match
    DECLARE @Start INT
    DECLARE @End INT
    -- length of current match
    DECLARE @CurLen INT
    -- Length of the total string -- 8001 if @InputString is NULL
    DECLARE @Len INT SET @Len = COALESCE(LEN(@InputString), 8001)

    WHILE (PATINDEX('%' + @Pattern + '%', @Result) != 0) 
    BEGIN
        SET @Replace = ''

        SET @Start = PATINDEX('%' + @Pattern + '%', @Result)
        SET @CurMatch = SUBSTRING(@Result, @Start, @Len)

        SET @End = PATINDEX('%[%]$%', @CurMatch) + 2
        SET @CurMatch = SUBSTRING(@CurMatch, 0, @End)

        SET @CurLen = LEN(@CurMatch)

        SET @Replace = REPLACE(RIGHT(@CurMatch, @CurLen - (PATINDEX('%##%', @CurMatch)+1)), '%
, '')

        SET @Result = REPLACE(@Result, @CurMatch, @Replace)
    END
    RETURN(@Result)
END

fixed it with this function:

CREATE FUNCTION [dbo].[ReplaceWithDefault]
(
   @InputString VARCHAR(4000)
)
RETURNS VARCHAR(4000)
AS
BEGIN
    DECLARE @Pattern VARCHAR(100) SET @Pattern = '$[%]_%##%[%]

    -- working copy of the string
    DECLARE @Result VARCHAR(4000) SET @Result = @InputString
    -- current match of the pattern
    DECLARE @CurMatch VARCHAR(500) SET @curMatch = ''
    -- string to replace the current match
    DECLARE @Replace VARCHAR(500) SET @Replace = ''
    -- start + end of the current match
    DECLARE @Start INT
    DECLARE @End INT
    -- length of current match
    DECLARE @CurLen INT
    -- Length of the total string -- 8001 if @InputString is NULL
    DECLARE @Len INT SET @Len = COALESCE(LEN(@InputString), 8001)

    WHILE (PATINDEX('%' + @Pattern + '%', @Result) != 0) 
    BEGIN
        SET @Replace = ''

        SET @Start = PATINDEX('%' + @Pattern + '%', @Result)
        SET @CurMatch = SUBSTRING(@Result, @Start, @Len)

        SET @End = PATINDEX('%[%]$%', @CurMatch) + 2
        SET @CurMatch = SUBSTRING(@CurMatch, 0, @End)

        SET @CurLen = LEN(@CurMatch)

        SET @Replace = REPLACE(RIGHT(@CurMatch, @CurLen - (PATINDEX('%##%', @CurMatch)+1)), '%
, '')

        SET @Result = REPLACE(@Result, @CurMatch, @Replace)
    END
    RETURN(@Result)
END
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文