替换重复出现的单词及其之前的字符
Replace a recurring word and the character before it
我正在使用 SQL 服务器尝试替换字符串中每个重复出现的“[BACKSPACE]”和出现在单词 [BACKSPACE] 之前的字符以模仿退格键的作用。
这是我当前的字符串:
"This is a string that I would like to d[BACKSPACE]correct and see if I could make it %[BACKSPACE] cleaner by removing the word and $[BACKSPACE] character before the backspace."
以下是我想表达的内容:
"This is a string that I would like to correct and see if I could make it cleaner by removing the word and character before the backspace."
让我更清楚地说明这一点。在上面的示例字符串中,$ 和 % 符号仅用作需要删除的字符示例,因为它们位于我要替换的 [BACKSPACE] 单词之前。
这是之前的另一个例子:
The dog likq[BACKSPACE]es it's owner
我想将其编辑为:
The dog likes it's owner
最后一个例子是:
I am frequesn[BACKSPACE][BACKSPACE]nlt[BACKSPACE][BACKSPACE]tly surprised
我想将其编辑为:
I am frequently surprised
如果没有提供 Regex 替换的 CLR 函数,您将能够执行此操作的唯一方法是在 T-SQL 中进行迭代。但是请注意,下面的解决方案 不会 为您提供您要求的结果,但会提供您要求的逻辑。您声明之前要删除字符串和字符,但在您的两种情况下情况并非如此。对于最后 2 个字符串,您分别删除 ' %[BACKSPACE]'
和 ' $[BACKSPACE]'
(注意前导空格)。
此解决方案中保留了前导空格。我不想解决这个问题,因为真正的解决方案是不要为此使用 T-SQL,使用支持 Regex 的东西。
我还假设这个字符串来自 table 中的一个列,并且说 table 有多行(每行的字符串都有不同的值)。
无论如何,解决方案:
WITH rCTE AS(
SELECT V.YourColumn,
STUFF(V.YourColumn,CHARINDEX('[BACKSPACE]',V.YourColumn)-1,LEN('[BACKSPACE]')+1,'') AS ReplacedColumn,
1 AS Iteration
FROM (VALUES('"This is a string that I would like to d[BACKSPACE]correct and see if I could make it %[BACKSPACE] cleaner by removing the word and $[BACKSPACE] character before the backspace."'))V(YourColumn)
UNION ALL
SELECT r.YourColumn,
STUFF(r.ReplacedColumn,CHARINDEX('[BACKSPACE]',r.ReplacedColumn)-1,LEN('[BACKSPACE]')+1,''),
r.Iteration + 1
FROM rCTE r
WHERE CHARINDEX('[BACKSPACE]',r.ReplacedColumn) > 0)
SELECT TOP (1) WITH TIES
r.YourColumn,
r.ReplacedColumn
FROM rCTE r
ORDER BY ROW_NUMBER() OVER (PARTITION BY r.YourColumn ORDER BY r.Iteration DESC);
这里有一个简单的 RegEx 模式应该可以工作:
/.\[BACKSPACE\]/g
编辑
我现在无法在我的 chromebook 上对此进行测试,但这似乎适用于 LIKE 子句
中的 T-SQL
LIKE '_\[BACKSPACE]' ESCAPE '\'
我想看看是否可以使用传统的 tally-table 方法而不使用任何递归来实现它。
我认为我有一些有用的东西 - 但是递归 cte 版本绝对是一个更干净的解决方案并且可能性能更好,但是将它作为一种替代的非递归方式投入使用。
/* tally table for use below */
select top 1000 N=Identity(int, 1, 1)
into dbo.Digits
from master.dbo.syscolumns a cross join master.dbo.syscolumns
with w as (
select seq = Row_Number() over (order by t.N),
part = Replace(Substring(@string, t.N, CharIndex(Left(@delimiter,1), @string + @delimiter, t.N) - t.N),Stuff(@delimiter,1,1,''),'')
from Digits t
where t.N <= DataLength(@string)+1 and Substring(Left(@delimiter,1) + @string, t.N, 1) = Left(@delimiter,1)
),
p as (
select seq,Iif(Iif(Lead(part) over(order by seq)='' and lag(part) over(order by seq)='',1,0 )=1 ,'', Iif( seq<Max(seq) over() and part !='',Left(part,Len(part)-1),part)) part
from w
)
select result=(
select ''+ part
from p
where part!=''
order by seq
for xml path('')
)
我正在使用 SQL 服务器尝试替换字符串中每个重复出现的“[BACKSPACE]”和出现在单词 [BACKSPACE] 之前的字符以模仿退格键的作用。
这是我当前的字符串:
"This is a string that I would like to d[BACKSPACE]correct and see if I could make it %[BACKSPACE] cleaner by removing the word and $[BACKSPACE] character before the backspace."
以下是我想表达的内容:
"This is a string that I would like to correct and see if I could make it cleaner by removing the word and character before the backspace."
让我更清楚地说明这一点。在上面的示例字符串中,$ 和 % 符号仅用作需要删除的字符示例,因为它们位于我要替换的 [BACKSPACE] 单词之前。
这是之前的另一个例子:
The dog likq[BACKSPACE]es it's owner
我想将其编辑为:
The dog likes it's owner
最后一个例子是:
I am frequesn[BACKSPACE][BACKSPACE]nlt[BACKSPACE][BACKSPACE]tly surprised
我想将其编辑为:
I am frequently surprised
如果没有提供 Regex 替换的 CLR 函数,您将能够执行此操作的唯一方法是在 T-SQL 中进行迭代。但是请注意,下面的解决方案 不会 为您提供您要求的结果,但会提供您要求的逻辑。您声明之前要删除字符串和字符,但在您的两种情况下情况并非如此。对于最后 2 个字符串,您分别删除 ' %[BACKSPACE]'
和 ' $[BACKSPACE]'
(注意前导空格)。
此解决方案中保留了前导空格。我不想解决这个问题,因为真正的解决方案是不要为此使用 T-SQL,使用支持 Regex 的东西。
我还假设这个字符串来自 table 中的一个列,并且说 table 有多行(每行的字符串都有不同的值)。
无论如何,解决方案:
WITH rCTE AS(
SELECT V.YourColumn,
STUFF(V.YourColumn,CHARINDEX('[BACKSPACE]',V.YourColumn)-1,LEN('[BACKSPACE]')+1,'') AS ReplacedColumn,
1 AS Iteration
FROM (VALUES('"This is a string that I would like to d[BACKSPACE]correct and see if I could make it %[BACKSPACE] cleaner by removing the word and $[BACKSPACE] character before the backspace."'))V(YourColumn)
UNION ALL
SELECT r.YourColumn,
STUFF(r.ReplacedColumn,CHARINDEX('[BACKSPACE]',r.ReplacedColumn)-1,LEN('[BACKSPACE]')+1,''),
r.Iteration + 1
FROM rCTE r
WHERE CHARINDEX('[BACKSPACE]',r.ReplacedColumn) > 0)
SELECT TOP (1) WITH TIES
r.YourColumn,
r.ReplacedColumn
FROM rCTE r
ORDER BY ROW_NUMBER() OVER (PARTITION BY r.YourColumn ORDER BY r.Iteration DESC);
这里有一个简单的 RegEx 模式应该可以工作:
/.\[BACKSPACE\]/g
编辑 我现在无法在我的 chromebook 上对此进行测试,但这似乎适用于 LIKE 子句
中的 T-SQLLIKE '_\[BACKSPACE]' ESCAPE '\'
我想看看是否可以使用传统的 tally-table 方法而不使用任何递归来实现它。
我认为我有一些有用的东西 - 但是递归 cte 版本绝对是一个更干净的解决方案并且可能性能更好,但是将它作为一种替代的非递归方式投入使用。
/* tally table for use below */
select top 1000 N=Identity(int, 1, 1)
into dbo.Digits
from master.dbo.syscolumns a cross join master.dbo.syscolumns
with w as (
select seq = Row_Number() over (order by t.N),
part = Replace(Substring(@string, t.N, CharIndex(Left(@delimiter,1), @string + @delimiter, t.N) - t.N),Stuff(@delimiter,1,1,''),'')
from Digits t
where t.N <= DataLength(@string)+1 and Substring(Left(@delimiter,1) + @string, t.N, 1) = Left(@delimiter,1)
),
p as (
select seq,Iif(Iif(Lead(part) over(order by seq)='' and lag(part) over(order by seq)='',1,0 )=1 ,'', Iif( seq<Max(seq) over() and part !='',Left(part,Len(part)-1),part)) part
from w
)
select result=(
select ''+ part
from p
where part!=''
order by seq
for xml path('')
)