Removing tags from a string

In http://ask.sqlteam.com, a question was asked about removing the comments from the HTML template

The comments have the following pattern

{/*}comment{*/}

The comments can be nested too

The task is to remove all the comments

Here is my solution

declare @string varchar(8000) ,@data varchar(8000)

select @string = 'AaaA{/*}XxxX{/*}YyyyY{*/}ZzzzZ{*/}BbbB{/*}CCC{*/}DdD'

select @string=replace(replace(@string, '{/*}','~{/*}'),'{*/}','~{*/}')

declare @t table(id int identity(1,1),data varchar(8000))

insert into @t(data)
select
 
        substring(@string, n, charindex('~', @string + '~', n ) - n
)
from

(
 
        select number as n from master..spt_values where type='p'

)
numbers
where
 
        substring( '~' + @string, n, 1 ) = '~'


--Method 1

set @string='' select @string =@string+data from
(
    select data from @t where id=

    union all
    select
substring(data,5,len(data)) from
@t 
    where (data like '{*/}%' and id%2=1
)
)
as t

select @string

--Method 2

select @string =''

select
        @string
=@string +substring(t2.data,5,len(t2.data))
from 
        @t
as t1 inner join @t as t2 on t1.id+1=t2.id
where 
        t2
.data like '{*/}%' and t2.id%2=1 and t2.data<>'{*/}'

select @string

Published Monday, October 12, 2009 2:28 PM by Madhivanan

Comments

# Parsing a string

Wednesday, November 18, 2009 9:19 AM by Madhivanan

Now-a-days lot of questions are aked in the forum about extracting particular text from a string Here

# Parsing a string

Wednesday, November 18, 2009 9:26 AM by SQL Server Transact-SQL (SSQA.net)

Now-a-days lot of questions are aked in the forum about extracting particular text from a string Here