Come posso sostituire i modelli dispari all'interno di una stringa?
-
21-12-2019 - |
Domanda
Sto creando una procedura temporanea in SQL perché ho un valore di una tabella che è scritto in markdown, quindi appare come HTML renderizzato nel browser web (ribasso per la conversione HTML).
La stringa della colonna attualmente assomiglia a questa:
Questions about **general computing hardware and software** are off-topic for Stack Overflow unless they directly involve tools used primarily for programming. You may be able to get help on [Super User](http://superuser.com/about)
Attualmente sto lavorando con testo in grassetto e corsivo.Questo significa (in caso di testo in grassetto) Dovrò sostituire N volte dispari il modello**
con<b>
e anche volte con</b>
.
vidi sostituire() ma esegue la sostituzione su tutti i pattern della corda.
Quindi, come posso sostituire una sottostringa solo se è dispari o solo pari?
Aggiornamento: Alcune persone si chiedono quali schemi sto usando, quindi dai un'occhiata Qui.
Un altro extra se vuoi: Il collegamento ipertestuale in stile markdown al collegamento ipertestuale html non sembra così semplice.
Soluzione
Usando ilSTUFF
funzione e un sempliceWHILE
ciclo continuo:
CREATE FUNCTION dbo.fn_OddEvenReplace(@text nvarchar(500),
@textToReplace nvarchar(10),
@oddText nvarchar(10),
@evenText nvarchar(500))
RETURNS varchar(max)
AS
BEGIN
DECLARE @counter tinyint
SET @counter = 1
DECLARE @switchText nvarchar(10)
WHILE CHARINDEX(@textToReplace, @text, 1) > 0
BEGIN
SELECT @text = STUFF(@text,
CHARINDEX(@textToReplace, @text, 1),
LEN(@textToReplace),
IIF(@counter%2=0,@evenText,@oddText)),
@counter = @counter + 1
END
RETURN @text
END
E puoi usarlo in questo modo:
SELECT dbo.fn_OddEvenReplace(column, '**', '<b>', '</b>')
FROM table
AGGIORNAMENTO:
Questo viene riscritto come SP:
CREATE PROC dbo.##sp_OddEvenReplace @text nvarchar(500),
@textToReplace nvarchar(10),
@oddText nvarchar(10),
@evenText nvarchar(10),
@returnText nvarchar(500) output
AS
BEGIN
DECLARE @counter tinyint
SET @counter = 1
DECLARE @switchText nvarchar(10)
WHILE CHARINDEX(@textToReplace, @text, 1) > 0
BEGIN
SELECT @text = STUFF(@text,
CHARINDEX(@textToReplace, @text, 1),
LEN(@textToReplace),
IIF(@counter%2=0,@evenText,@oddText)),
@counter = @counter + 1
END
SET @returnText = @text
END
GO
E per eseguire:
DECLARE @returnText nvarchar(500)
EXEC dbo.##sp_OddEvenReplace '**a** **b** **c**', '**', '<b>', '</b>', @returnText output
SELECT @returnText
Altri suggerimenti
Secondo la richiesta di OP ho modificato la mia risposta precedente per eseguirla come procedura memorizzata temporanea.Ho lasciato la mia risposta precedente poiché ritengo utile anche l'utilizzo contro una tabella di stringhe.
Se si sa che esiste già una tabella Tally (o Numbers) con almeno 8000 valori, è possibile omettere la sezione contrassegnata del CTE e il riferimento CTE conteggio sostituito con il nome della tabella Tally esistente.
create procedure #HtmlTagExpander(
@InString varchar(8000)
,@OutString varchar(8000) output
)as
begin
declare @Delimiter char(2) = '**';
create table #t(
StartLocation int not null
,EndLocation int not null
,constraint PK unique clustered (StartLocation desc)
);
with
-- vvv Only needed in absence of Tally table vvv
E1(N) as (
select 1 from (values
(1),(1),(1),(1),(1),
(1),(1),(1),(1),(1)
) E1(N)
), --10E+1 or 10 rows
E2(N) as (select 1 from E1 a cross join E1 b), --10E+2 or 100 rows
E4(N) As (select 1 from E2 a cross join E2 b), --10E+4 or 10,000 rows max
tally(N) as (select row_number() over (order by (select null)) from E4),
-- ^^^ Only needed in absence of Tally table ^^^
Delimiter as (
select len(@Delimiter) as Length,
len(@Delimiter)-1 as Offset
),
cteTally(N) AS (
select top (isnull(datalength(@InString),0))
row_number() over (order by (select null))
from tally
),
cteStart(N1) AS
select
t.N
from cteTally t cross join Delimiter
where substring(@InString, t.N, Delimiter.Length) = @Delimiter
),
cteValues as (
select
TagNumber = row_number() over(order by N1)
,Location = N1
from cteStart
),
HtmlTagSpotter as (
select
TagNumber
,Location
from cteValues
),
tags as (
select
Location = f.Location
,IsOpen = cast((TagNumber % 2) as bit)
,Occurrence = TagNumber
from HtmlTagSpotter f
)
insert #t(StartLocation,EndLocation)
select
prev.Location
,data.Location
from tags data
join tags prev
on prev.Occurrence = data.Occurrence - 1
and prev.IsOpen = 1;
set @outString = @Instring;
update this
set @outString = stuff(stuff(@outString,this.EndLocation, 2,'</b>')
,this.StartLocation,2,'<b>')
from #t this with (tablockx)
option (maxdop 1);
end
go
Invocato in questo modo:
declare @InString varchar(8000)
,@OutString varchar(8000);
set @inString = 'Questions about **general computing hardware and software** are off-topic **for Stack Overflow.';
exec #HtmlTagExpander @InString,@OutString out; select @OutString;
set @inString = 'Questions **about** general computing hardware and software **are off-topic** for Stack Overflow.';
exec #HtmlTagExpander @InString,@OutString out; select @OutString;
go
drop procedure #HtmlTagExpander;
go
Restituisce come output:
Questions about <b>general computing hardware and software</b> are off-topic **for Stack Overflow.
Questions <b>about</b> general computing hardware and software <b>are off-topic</b> for Stack Overflow.
Un'opzione è utilizzare un'espressione regolare in quanto rende molto semplice la sostituzione di tali modelli.Le funzioni RegEx non sono integrate in SQL Server, quindi è necessario utilizzare SQL CLR, compilato dall'utente o da una libreria esistente.
Per questo esempio userò il file SQL# (SQLsharp) (di cui sono autore) ma le funzioni RegEx sono disponibili nella versione Free.
SELECT SQL#.RegEx_Replace
(
N'Questions about **general computing hardware and software** are off-topic\
for Stack Overflow unless **they** directly involve tools used primarily for\
**programming. You may be able to get help on [Super User]\
(https://superuser.com/about)', -- @ExpressionToValidate
N'\*\*([^\*]*)\*\*', -- @RegularExpression
N'<b>$1</b>', -- @Replacement
-1, -- @Count (-1 = all)
1, - @StartAt
'IgnoreCase' -- @RegEx options
);
Il modello sopra \*\*([^\*]*)\*\*
cerca semplicemente qualsiasi cosa circondata da doppi asterischi.In questo caso non devi preoccuparti dei pari/dispari.Significa anche che non otterrai un risultato mal formato <b>
-tag solo se per qualche motivo c'è un extra **
nella corda.Ho aggiunto due ulteriori casi di test alla stringa originale:un set completo di **
intorno alla parola they
e un set senza eguali di **
subito prima della parola programming
.L'output è:
Questions about <b>general computing hardware and software</b> are off-topicfor Stack Overflow unless <b>they</b> directly involve tools used primarily for **programming. You may be able to get help on [Super User](https://superuser.com/about)
che viene reso come:
Domande a proposito di hardware e software informatici generali sono fuori tema per Stack Overflow a meno che Essi coinvolgono direttamente strumenti utilizzati principalmente per la **programmazione.Potresti essere in grado di ottenere aiuto Superutente
Questa soluzione si avvale delle tecniche descritte da Jeff Moden in questo articolo sul problema della somma corrente in SQL.Questa soluzione è lunga, ma utilizzando il file Aggiornamento bizzarro in SQL Server su un indice cluster, promette di essere molto più efficiente su set di dati di grandi dimensioni rispetto alle soluzioni basate su cursore.
Aggiornamento - modificato di seguito per operare su una tabella di stringhe
Supponendo l'esistenza di una tabella tally creata in questo modo (con almeno 8000 righe):
create table dbo.tally (
N int not null
,unique clustered (N desc)
);
go
with
E1(N) as (
select 1 from (values
(1),(1),(1),(1),(1),
(1),(1),(1),(1),(1)
) E1(N)
), --10E+1 or 10 rows
E2(N) as (select 1 from E1 a cross join E1 b), --10E+2 or 100 rows
E4(N) As (select 1 from E2 a cross join E2 b) --10E+4 or 10,000 rows max
insert dbo.tally(N)
select row_number() over (order by (select null)) from E4;
go
e un HtmlTagSpotter funzione definita in questo modo:
create function dbo.HtmlTagSPotter(
@pString varchar(8000)
,@pDelimiter char(2))
returns table with schemabinding as
return
WITH
Delimiter as (
select len(@pDelimiter) as Length,
len(@pDelimiter)-1 as Offset
),
cteTally(N) AS (
select top (isnull(datalength(@pstring),0))
row_number() over (order by (select null))
from dbo.tally
),
cteStart(N1) AS (--==== Returns starting position of each "delimiter" )
select
t.N
from cteTally t cross join Delimiter
where substring(@pString, t.N, Delimiter.Length) = @pDelimiter
),
cteValues as (
select
ItemNumber = row_number() over(order by N1)
,Location = N1
from cteStart
)
select
ItemNumber
,Location
from cteValues
go
quindi l'esecuzione del seguente SQL eseguirà la sostituzione richiesta.Tieni presente che l'inner join alla fine impedisce la conversione di eventuali tag "strani" finali:
create table #t(
ItemNo int not null
,Item varchar(8000) null
,StartLocation int not null
,EndLocation int not null
,constraint PK unique clustered (ItemNo,StartLocation desc)
);
with data(i,s) as ( select i,s from (values
(1,'Questions about **general computing hardware and software** are off-topic **for Stack Overflow.')
,(2,'Questions **about **general computing hardware and software** are off-topic **for Stack Overflow.')
--....,....1....,....2....,....3....,....4....,....5....,....6....,....7....,....8....,....9....,....0
)data(i,s)
),
tags as (
select
ItemNo = data.i
,Item = data.s
,Location = f.Location
,IsOpen = cast((TagNumber % 2) as bit)
,Occurrence = TagNumber
from data
cross apply dbo.HtmlTagSPotter(data.s,'**') f
)
insert #t(ItemNo,Item,StartLocation,EndLocation)
select
data.ItemNo
,data.Item
,prev.Location
,data.Location
from tags data
join tags prev
on prev.ItemNo = data.ItemNo
and prev.Occurrence = data.Occurrence - 1
and prev.IsOpen = 1
union all
select
i,s,8001,8002
from data
;
declare @ItemNo int
,@ThisStting varchar(8000);
declare @s varchar(8000);
update this
set @s = this.Item = case when this.StartLocation > 8000
then this.Item
else stuff(stuff(@s,this.EndLocation, 2,'</b>')
,this.StartLocation,2,'<b>')
end
from #t this with (tablockx)
option (maxdop 1);
select
Item
from (
select
Item
,ROW_NUMBER() over (partition by ItemNo order by StartLocation) as rn
from #t
) t
where rn = 1
go
cedendo:
Item
------------------------------------------------------------------------------------------------------------
Questions about <b>general computing hardware and software</b> are off-topic **for Stack Overflow.
Questions <b>about </b>general computing hardware and software<b> are off-topic </b>for Stack Overflow.