久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何在 SQL Server 中替換正則表達式 HTML 標簽?

How to do regex HTML tag replace in SQL Server?(如何在 SQL Server 中替換正則表達式 HTML 標簽?)
本文介紹了如何在 SQL Server 中替換正則表達式 HTML 標簽?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我在 SQL Server 2005 中有一個表,其中包含數百行 HTML 內容.某些內容具有 HTML,例如:

I have a table in SQL Server 2005 with hundreds of rows with HTML content. Some of the content has HTML like:

<span class=heading-2>Directions</span>

方向"隨頁面名稱而變化.

where "Directions" changes depending on page name.

我需要將所有 標簽更改為

> 和

標簽.

I need to change all the <span class=heading-2> and </span> tags to <h2> and </h2> tags.

我過去編寫此查詢是為了進行內容更改,但由于結束 HTML 標記,它不適用于我當前的問題:

I wrote this query to do content changes in the past, but it doesn't work for my current problem because of the ending HTML tag:

Update  ContentManager
Set ContentManager.Content = replace(Cast(ContentManager.Content AS NVARCHAR(Max)), 'old text', 'new text')

有誰知道我怎樣才能完全在 T-SQL 中實現到 h2 的替換?我發現的一切都表明我必須進行 CLR 集成.謝謝!

Does anyone know how I could accomplish the span to h2 replacing purely in T-SQL? Everything I found showed I would have to do CLR integration. Thanks!

推薦答案

確實,T-SQL 本身并不支持正則表達式,在這種問題中,正則表達式將成為首選工具.首先,我會說解決方案的復雜程度在很大程度上取決于您的數據的一致性.例如,假設我們搜索具有以下標題的項目:

Indeed T-SQL does not natively support regular expressions and this is the sort of problem in which regular expressions would be the tool of choice. First, I'll say that the level of complication in the solution depends greatly on how consistent your data is. For example, suppose we search for items with the heading:

Select ..
From ...
Where HtmlContent Like '<span class="heading-2">%'

這假定 spanclass 之間沒有額外的間距,并且在結束括號之前的最后一個雙引號之后沒有額外的間距.我們可以編寫 '%<span%class="heading-2"%>%' 來說明空格,但也會發現 div 標記為 heading-2 在與任何 span 標簽相同的內容中.如果后面的場景不應該發生,但你可能有不同的空間,那么使用這個修改后的模式.我們真正會遇到麻煩的是結束標簽.假設我們的內容如下所示:

This assumes no additional spacing between span and class as well as no additional spacing after the final double quote before the end bracket. We could write '%<span%class="heading-2"%>%' to account for the spaces but that would also find div tags marked as heading-2 in the same content as any span tag. If this later scenario shouldn't happen but you might have varying spaces, then use this revised pattern. Where we will really run into troubles is the closing tag. Suppose our content looks like so:

<span class="heading-2"> Foo <span class="heading-3">Bar</span> And Gamma Too</span> .... <span class="heading-4">Fubar Is the right way!</span>...

找到正確的結束 span 標記以更改為 </h2> 并不是那么簡單.您不能簡單地找到第一個 并將其更改為 .如果你知道你沒有嵌套的 span 標簽,那么你可以編寫一個用戶定義的函數來做到這一點:

It is not so simple to find the correct closing span tag to change to </h2>. You cannot simply find the first </span> and change it to </h2>. If you knew that you had no nested span tags, then you could write a user-defined function that would do it:

Create Function ReplaceSpanToH2( @HtmlContent nvarchar(max) )
Returns nvarchar(max)
As
Begin
    Declare @StartPos int
    Declare @EndBracket int

    Set @StartPos = CharIndex('<span class="heading-2">', @HtmlContent)
    If @StartPos = 0
        Return @HtmlContent

    Set @HtmlContent = Replace(@HtmlContent, '<span class="heading-2">', '<h2>')

    -- find next </span>
    Set @StartPos = CharIndex('</span>', @HtmlContent, @StartPos)

    Set @HtmlContent = Stuff(@HtmlContent, @StartPos, 7, '</h2>')
    Return @HtmlContent
End

這篇關于如何在 SQL Server 中替換正則表達式 HTML 標簽?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Converting Every Child Tags in to a Single Column with multiple Delimiters -SQL Server (3)(將每個子標記轉換為具有多個分隔符的單列-SQL Server (3))
How can I create a view from more than one table?(如何從多個表創建視圖?)
Create calculated value based on calculated value inside previous row(根據前一行內的計算值創建計算值)
How do I stack the first two columns of a table into a single column, but also pair third column with the first column only?(如何將表格的前兩列堆疊成一列,但也僅將第三列與第一列配對?) - IT屋-程序員軟件開發技
Recursive t-sql query(遞歸 t-sql 查詢)
Convert Month Name to Date / Month Number (Combinations of Questions amp; Answers)(將月份名稱轉換為日期/月份編號(問題和答案的組合))
主站蜘蛛池模板: 自拍 亚洲 欧美 老师 丝袜 | 在线看片国产精品 | 日韩在线观看一区 | 午夜免费av| 黄色成人免费看 | 国产精品一区二区日韩 | 中文字幕 国产 | 久久高清 | 一区二区三区在线 | 精品久久久久久亚洲精品 | 精品欧美一区二区中文字幕视频 | 欧美综合久久久 | 免费在线观看黄视频 | 久久久www成人免费精品 | 国产精品三级久久久久久电影 | 中文字幕一区二区三区不卡 | 亚洲网站在线观看 | 欧美日韩亚洲成人 | 久久精品亚洲 | 99久久免费精品国产男女高不卡 | 欧美久久久久 | 中文字幕日韩欧美一区二区三区 | 成人欧美一区二区三区黑人孕妇 | 国产丝袜av | 欧美综合久久久 | 午夜免费福利电影 | 久久精品国产一区二区 | 精品国产乱码久久久久久老虎 | 国产剧情一区 | 欧美成人精品在线 | 国产亚洲精品久久久久久豆腐 | 韩国精品在线观看 | 国产成人免费视频网站高清观看视频 | 在线国产精品一区 | 国产精品女人久久久 | 日韩在线91 | 九色www| 日韩成人免费视频 | 91久久久久久久久 | 国产第一区二区 | 国产亚洲一区二区精品 |