国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

T-SQL中的正則表達(dá)式

2024-07-21 02:08:22
字體:
供稿:網(wǎng)友
中國最大的web開發(fā)資源網(wǎng)站及技術(shù)社區(qū),

t-sql中的正則表達(dá)式


原作者:cory koski
發(fā)表時(shí)間:2003/06/24


        本文來自cory koski。cory寫道:“我最近遇到一個(gè)問題,就是試圖在數(shù)據(jù)庫域中搜索一個(gè)正則表達(dá)式。還沒有一個(gè)版本的sqlserver內(nèi)部支持正則表達(dá)式,但我發(fā)現(xiàn)了一個(gè)將正則表達(dá)式的所有優(yōu)點(diǎn)添加到你的t_sql應(yīng)用的方法。為了更容易的使用正則表達(dá)式,我們可以使用自定義函數(shù)(user defined function, udf)來幫助我們并使工作簡潔。”

在這個(gè)解決方案中,我們需要sql server 2000或更高。我們還需要確定機(jī)器中有vbscript.regexp類庫,這隨大多數(shù)windows 2000 servers中的windows scripting包配有。若你正在使用一個(gè)更早版本的windows,你必須為你的操作系統(tǒng)下載最新版的windows scripting。
自定義函數(shù)
下面是我的自定義函數(shù),可用來在源字符串中搜索一個(gè)正則模式表達(dá)式。
create function dbo.find_regular_expression
 (
  @source varchar(5000),
  @regexp varchar(1000),
  @ignorecase bit = 0
 )
returns bit
as
 begin
  declare @hr integer
  declare @objregexp integer
  declare @objmatches integer
  declare @objmatch integer
  declare @count integer
  declare @results bit
  
  exec @hr = sp_oacreate 'vbscript.regexp', @objregexp output
  if @hr <> 0 begin
   set @results = 0
   return @results
  end
  exec @hr = sp_oasetproperty @objregexp, 'pattern', @regexp
  if @hr <> 0 begin
   set @results = 0
   return @results
  end
  exec @hr = sp_oasetproperty @objregexp, 'global', false
  if @hr <> 0 begin
   set @results = 0
   return @results
  end
  exec @hr = sp_oasetproperty @objregexp, 'ignorecase', @ignorecase
  if @hr <> 0 begin
   set @results = 0
   return @results
  end 
  exec @hr = sp_oamethod @objregexp, 'test', @results output, @source
  if @hr <> 0 begin
   set @results = 0
   return @results
  end
  exec @hr = sp_oadestroy @objregexp
  if @hr <> 0 begin
   set @results = 0
   return @results
  end
 return @results
 end

將這個(gè)udf保存到你的數(shù)據(jù)庫中,并確定有授權(quán)來運(yùn)行它。當(dāng)然,你也得確保運(yùn)行它的人有運(yùn)行sp_oaxxxxx類擴(kuò)展存儲(chǔ)過程的權(quán)限。
這個(gè)函數(shù)已確保正常運(yùn)行,并且即便是和com對象一起使用,也還是挺快的。

舉例
使用正則表達(dá)式的一個(gè)地方就是測試特殊字符。我們不搜索所有的特殊字符,而是查找正常字符的匹配項(xiàng),例如字母和空格。我們看看它的運(yùn)行。
declare @intlength as integer
declare @vchregularexpression as varchar(50)
declare @vchsourcestring as varchar(50)
declare @vchsourcestring2 as varchar(50)
declare @bithasnospecialcharacters as bit

-- 初始化變量
set @vchsourcestring = 'test one this is a test!!'
set @vchsourcestring2 = 'test two this is a test'

-- 我們的正則表達(dá)式應(yīng)該類似于
-- [a-za-z ]{}
-- 如: [a-za-z ]{10}  ...  一個(gè)十字符的字符串

-- 獲得字符串長度
set @intlength = len(@vchsourcestring)

-- 設(shè)置完整的正則表達(dá)式
set @vchregularexpression = '[a-za-z ]{' +
cast(@intlength as varchar) + '}'

-- 是否有任何特殊字符
set @bithasnospecialcharacters = dbo.find_regular_expression(
@vchsourcestring, @vchregularexpression,0)

print @vchsourcestring
if @bithasnospecialcharacters = 1 begin
 print 'no special characters.'
end else begin
 print 'special characters found.'
end

print '---'

-- 獲得字符串長度
set @intlength = len(@vchsourcestring2)

-- 設(shè)置完整的正則表達(dá)式
set @vchregularexpression = '[a-za-z ]{' +
cast(@intlength as varchar) + '}'

-- 是否有任何特殊字符
set @bithasnospecialcharacters = dbo.find_regular_expression(
@vchsourcestring2, @vchregularexpression,0)

print @vchsourcestring2
if @bithasnospecialcharacters = 1 begin
 print 'no special characters.'
end else begin
 print 'special characters found.'
end

go
the results for this example would be:
本例的結(jié)果應(yīng)該是:
test one this is a test!!
special characters found.
---
test two this is a test
no special characters.

結(jié)論:
正如你所見,這是一個(gè)簡單技巧,在特定的場合得到了非常有用的結(jié)果。你作為一個(gè)t_sql開發(fā)人員,可以在正則表達(dá)式庫vbscript.regexp中使用和擴(kuò)展這個(gè)技巧。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 连云港市| 茶陵县| 长春市| 海伦市| 富裕县| 监利县| 新竹县| 龙泉市| 安龙县| 长兴县| 黄陵县| 塘沽区| 桃源县| 西丰县| 廉江市| 原平市| 关岭| 东源县| 海林市| 大新县| 泽州县| 仪征市| 连平县| 凤城市| 从江县| 遂宁市| 甘孜县| 余江县| 泗水县| 遵化市| 合阳县| 镇平县| 苍南县| 东丽区| 分宜县| 罗平县| 碌曲县| 晋州市| 浦北县| 新津县| 卫辉市|