我想找到patternin string并用其他方式替换该模式 [英] I wanted find patternin string and replace that pattern wiht other

查看:65
本文介绍了我想找到patternin string并用其他方式替换该模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的先生,





i想要在字符串中找到特定的图案并将其替换为另一种图案

例如:



string ='a,b,c:d,e,f:'



findpattern:



replacepattern:,:



输出将是a,b,c,: d,e,f,:



我尝试过:



Dear sir,


i wanted find particular pattern in string and replace the same with another pattern
for example:

string='a,b,c: d, e, f :'

findpattern :

replacepattern: ,:

output will be a,b,c,: d, e, f ,:

What I have tried:

Alter FUNCTION dbo.RecursiveReplace_fn1
(
    @P_String VARCHAR(MAX),
    @P_Pattern VARCHAR(MAX),
    @P_ReplaceString VARCHAR(MAX),
    @P_ReplaceLength INT = 1
)
RETURNS VARCHAR(MAX)
BEGIN
    DECLARE @Index INT;

    -- Get starting point of pattern
    SET @Index = PATINDEX(@P_Pattern, @P_String);

    IF @Index > 0
    BEGIN
        -- Perform the replace
        SET @P_String = STUFF(@P_String, PATINDEX(@P_Pattern, @P_String), @P_ReplaceLength, @P_ReplaceString);

        -- Recurse
        SET @P_String = dbo.RecursiveReplace_fn1(@P_String, @P_Pattern, @P_ReplaceString, @P_ReplaceLength);
    END;
    
    RETURN @P_String;
END;

推荐答案

7小时前你问过一个非常类似的问题:动态操作字符串函数可能会传递参数字符串需要在给定位置的字符串中添加任何字符 [ ^ ] I告诉你,你的整个方法都是错的:你现在开始明白为什么你的整个存储设备都有问题吗?



这个问题不会再好了 - 它会发生变得更糟。在你成为一个巨大的PITA的道路上之前,帮自己一个忙,改变你的数据库。
When you asked a very similar question 7 hours ago: Sql fuction manipulate string dynamically may passing parameter string needs to add any character in string given position[^] I told you that your whole approach was wrong: do you now start to see why your whole storage desiogn is faulty?

This problem won't get any better - it's going to get worse instead. Do yourself a favour and change your DB before you get so far down the road that it becomes a huge PITA.


这篇关于我想找到patternin string并用其他方式替换该模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆