程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> ASP編程 >> ASP技巧 >> 正則表達式結合數組提取文章中的文件名

正則表達式結合數組提取文章中的文件名

編輯:ASP技巧

今天編ZBlog上傳模塊的時候,需要用到一個提取文章中文件名的子程,開始我把問題想復雜了,匹配了所有可能的文件名,不僅正則表達式寫了一大串,而且下面還Split了半天,後來我突然發現Z-Blog的上傳文件都存在upload下,白寫了那麼多復雜的匹配。。。哎,不能浪費掉啊,還是貼上來萬一哪個兄弟以後需要也好拿去用~~

下面這段子程基本上可以算是比較不錯的通用匹配了。(PS:我突然發現CODE_LITE把我的UBB轉義了!!!暈,我只好自己轉義了。。。)


--------------------------------------------------------------------------------
Dim objRegExp,Matches,i,DC9_DOT_CN_MATCH
Dim aryMatch()
Redim PReserve aryMatch(0)
Set objRegExp=New RegExp
objRegExp.IgnoreCase =True
objRegExp.Global=True
objRegExp.Pattern="(?:\[[^\]]+\]([^\[]+)\[\/[^\]]+\])|(?:(?:href|src)=([^\s|^>]+)[""|>|\s\'])"
Set Matches = objRegExp.Execute("[img]2312.jpg[/img][img]2312.jpg[/img]hh[img]http://www.dc9.cn/cm/upload/2ed312.jpg[/img]<a href=""http://www.dc9.cn/ddd.jpg""></a> href=""[IMG_RIGHT=400,300,title]upload/2assas.jpg[/IMG_RIGHT]http://www.dc9.cn/dccdd.jpg"">[IMG_RIGHT=400,300,title]upload/2assas.jpg[/IMG_RIGHT]<sctipt src=""http://www.dc9.cn/upload/sss.jpg""")
Dim TmpMatch
For i=0 to Matches.Count-1
Call InsertDataToArray(Matches(i).SubMatches(0),aryMatch)
Call InsertDataToArray(Matches(i).SubMatches(1),aryMatch)
Next

For i=0 to Ubound(aryMatch)
If Not IsNull(aryMatch(i)) And Trim(aryMatch(i))<>"" Then Response.write aryMatch(i)&"<br>"
Next

Function InsertDataToArray(Data,ByRef aryMatch)
If Trim(Data)<>"" Then
Data=Replace(Data,"'","")
Data=Replace(Data,"""","")
Data=Replace(Data,"\","/")
Data=Split(Data,"/")(Ubound(Split(Data,"/")))
Redim Preserve aryMatch(Ubound(aryMatch)+1)
DC9_DOT_CN_MATCH=False
For j=0 to Ubound(aryMatch)
If aryMatch(j)=Data Then DC9_DOT_CN_MATCH=True
Next
If Not DC9_DOT_CN_MATCH Then aryMatch(Ubound(aryMatch))=Data
End If
End Function

--------------------------------------------------------------------------------

實際上,Z-Blog的話就匹配Upload就可以了(但是我為了省事,也為了更精確些,就干脆在上面的那個匹配上面加上了upload,所以看上去好像下面這個更復雜些,其實理論上應該下面這個簡單些,畢竟有upload管著呢。。),也不用SPLit那個/.這麼寫就可以了:


--------------------------------------------------------------------------------
Dim objRegExp,Matches,i,ZC_UPLOAD_MATCH
Dim aryMatch()
Redim Preserve aryMatch(0)
Set objRegExp=New RegExp
objRegExp.IgnoreCase =True
objRegExp.Global=True
objRegExp.Pattern="(?:\[[^\]]+\][^\[]*upload\/([^\[|^\\|^\/]+)\[\/[^\]]+\])|(?:(?:href|src)=""{0,1}[^\s|^""|^>|^']*upload\/([^\s|^>]+)[""|>|\s\'])"
Set Matches = objRegExp.Execute("[img]2312.jpg[/img][img]2312.jpg[/img]hh[img]http://www.dc9.cn/cm/upload/2ed312.jpg[/img]<a href=""http://www.dc9.cn/ddd.jpg""></a> href=""[IMG_RIGHT=400,300,title]upload/2assas.jpg[/IMG_RIGHT]http://www.dc9.cn/dccdd.jpg"">[IMG_RIGHT=400,300,title]upload/2assas.jpg[/IMG_RIGHT]<sctipt src=""http://www.dc9.cn/upload/sss.jpg"" src=http://www.dc9.cn/upload/sasds.jpg'>")
Dim TmpMatch
For i=0 to Matches.Count-1
Call InsertDataToArray(Matches(i).SubMatches(0),aryMatch)
Call InsertDataToArray(Matches(i).SubMatches(1),aryMatch)
Next

For i=0 to Ubound(aryMatch)
If Not IsNull(aryMatch(i)) And Trim(aryMatch(i))<>"" Then Response.write aryMatch(i)&"<br>"
Next

Function InsertDataToArray(Data,ByRef aryMatch)
If Trim(Data)<>"" Then
Data=Replace(Data,"'","")
Data=Replace(Data,"""","")
Redim Preserve aryMatch(Ubound(aryMatch)+1)
ZC_UPLOAD_MATCH=False
For j=0 to Ubound(aryMatch)
If aryMatch(j)=Data Then ZC_UPLOAD_MATCH=True
Next
If Not ZC_UPLOAD_MATCH Then aryMatch(Ubound(aryMatch))=Data
End If
End Function

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved