Fun*_*unc 6 regex sql t-sql sql-server-2008
我需要提取一个被***[some text]字符串包围的文本,如下例所示:
some text
some text
***[some text]
THIS SHOULD BE EXTRACTED
***[some text]
some text
some text
some text
some text
some text
***[some text]
THIS SHOULD BE EXTRACTED TOO
***[some text]
some text
Run Code Online (Sandbox Code Playgroud)
输出应该是:
THIS SHOULD BE EXTRACTED
THIS SHOULD BE EXTRACTED TOO
Run Code Online (Sandbox Code Playgroud)
我PATINDEX在这里试过,但是找不到提取字符串的方法.
PATINDEX('%[*][*][*][[]%]%%[*][*][*][[]%]%',@Text)
Run Code Online (Sandbox Code Playgroud)
我期待听到任何建议.
不是一个正则表达式解决方案,我仍然是一个 SQL 新手,所以可能不是最佳的,但你应该能够使用循环进行WHILE解析
CHARINDEXfor***然后使用它作为起点 to
CHARINDEX使用LF它作为 a 的起点,
SUBSTRING终点是CHARINDEX下一个的***
a 将子字符串连接到输出,移过结尾***并循环查找下一个。
我会尝试一下,看看是否可以添加一个例子。
编辑-这可能需要更多的错误检查
declare @inText nvarchar(2000) = 'some text
some text
***[some text]
THIS SHOULD BE EXTRACTED
***[some text]
some text
some text
some text
some text
some text
***[some text]
THIS SHOULD BE EXTRACTED TOO
***[some text]
some text '
declare @delim1 nvarchar(50) = '***'
declare @delim2 char = char(10)
declare @output nvarchar(1000) = ''
declare @position int
declare @positionEnd int
set @position = CHARINDEX(@delim1,@inText)
while (@position != 0 and @position is not null)
BEGIN
set @position = CHARINDEX(@delim2,@inText,@position)
set @positionEnd = CHARINDEX(@delim1,@inText,@position)
set @output = @output + SUBSTRING(@inText,@position,@positionEnd-@position)
set @position = CHARINDEX(@delim1,@inText,@positionEnd+LEN(@delim1))
END
select @output
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15364 次 |
| 最近记录: |