查找 2 个字符串之间的文本

Find text in between 2 strings

我有以下代码可用于在 2 个字符串之间查找文本。我需要帮助查找多次出现的文本。在这种情况下,我试图在开始和结束之间找到文本。在这种情况下,它 returns 一、二、三正确。

 DECLARE @text AS VARCHAR(MAX) = 'this is for first <start> one, two, three <end>.';
 DECLARE @pretext AS nvarchar(100) = '<start>'    
 DECLARE @posttext AS nvarchar(100) = '<end>'

 SELECT 
  CASE 
     WHEN CHARINDEX(@posttext, @Text) - (CHARINDEX(@pretext, @Text) + len(@pretext)) < 0 
        THEN ''
        ELSE SUBSTRING(@Text, 
                       CHARINDEX(@pretext, @Text) + LEN(@pretext),   
                       CHARINDEX(@posttext, @Text) - (CHARINDEX(@pretext, @Text) + 
 LEN(@pretext)))    
 END AS textinbetween   

接下来,如果我有如下字符串:

  DECLARE @text AS VARCHAR(MAX) = 'this <end><end>is for first <start> one, two, three <end>.this is for second <start> four, five, six<end>.';

基本上,我需要介于和之间的所有文本。在本例中有 2 个:一、二、三和四、五、六。我可以有任何 x 数量,所以只需要在所有这些之间获取该文本。

提前致谢

在 T-SQL 中做这件事很丑陋,因为开始和结束都是“错误的”。也许是 CTE。我更新了测试字符串以包含错误的开始。如果要在结果中排​​除错误的开始和结束标记,可以在最后的子字符串之后替换它们。

DECLARE @text AS VARCHAR(MAX) = 'this <end><end>is for first <start> one, two, three <end>.this is for second <start> four, five, <start>six<start><end>.';
DECLARE @pretext as VARCHAR(128) = '<start>'
DECLARE @posttext as VARCHAR(128) = '<end>';

WITH Tokens as (
    SELECT CHARINDEX(@pretext, @Text, 1) as [startIndex], 
        CHARINDEX(@posttext, @Text, CHARINDEX(@pretext, @Text, 1)) as [stopIndex],
        1 as [TokenNumber]
    WHERE CHARINDEX(@pretext, @Text, 1) > 0 
        and CHARINDEX(@posttext, @Text, CHARINDEX(@pretext, @Text, 1)) > 0
    UNION ALL 
    SELECT CHARINDEX(@pretext, @Text, t.[stopIndex]) as [startIndex], 
        CHARINDEX(@posttext, @Text, CHARINDEX(@pretext, @Text, CHARINDEX(@pretext, @Text, t.[stopIndex]))) as [stopIndex],
        t.TokenNumber + 1
    FROM Tokens t
    WHERE CHARINDEX(@pretext, @Text, t.[stopIndex]) > 0 
        AND CHARINDEX(@posttext, @Text, CHARINDEX(@pretext, @Text, CHARINDEX(@pretext, @Text, t.[stopIndex]))) > 0
) SELECT *, 
    SUBSTRING(@Text, [startIndex] + LEN(@pretext), [stopIndex] - [startIndex] - LEN(@pretext)) 
    FROM Tokens

假设每个值都在其自己的行中是可以的 string_split 是一个简单的选择。如果您需要将其放在一行中,有数以千计的示例说明如何旋转或使用东西将它们返回到一行。

declare @asdf varchar(100) = 'This is a test <one>first string<two> and this is the second <one>second string<two>.'

select left(x.value, charindex('<two>', x.value) - 1)
from string_split(replace(@asdf, '<one>', '^'), '^') x --The delimiter is limited to a single character so just used replace to switch it for a character not in your string.
where charindex('<two>', x.value) > 0