正则表达式结合数组提取文章中的文件名
类别: ASP教程
今天编Blog上传模块的时候,需要用到一个提取文章中文件名的子程,开始我把问题想复杂了,匹配了所有可能的文件名,不仅正则表达式写了一大串,而且下面还Split了半天,后来我突然发现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.etoow.com/cm/upload/2ed312.jpg[/img]<a href=""http://www.etoow.com/ddd.jpg""></a> href=""http://www.etoow.com/dccdd.jpg"">[IMG_RIGHT=400,300,title]upload/2assas.jpg[/IMG_RIGHT]<sctipt src=""http://www.etoow.com/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.etoow.com/cm/upload/2ed312.jpg[/img]<a href=""http://www.etoow.com/ddd.jpg""></a> href=""http://www.etoow.com/dccdd.jpg"">[IMG_RIGHT=400,300,title]upload/2assas.jpg[/IMG_RIGHT]<sctipt src=""http://www.etoow.com/upload/sss.jpg"" src=http://www.etoow.com/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
下面这段子程基本上可以算是比较不错的通用匹配了。(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.etoow.com/cm/upload/2ed312.jpg[/img]<a href=""http://www.etoow.com/ddd.jpg""></a> href=""http://www.etoow.com/dccdd.jpg"">[IMG_RIGHT=400,300,title]upload/2assas.jpg[/IMG_RIGHT]<sctipt src=""http://www.etoow.com/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.etoow.com/cm/upload/2ed312.jpg[/img]<a href=""http://www.etoow.com/ddd.jpg""></a> href=""http://www.etoow.com/dccdd.jpg"">[IMG_RIGHT=400,300,title]upload/2assas.jpg[/IMG_RIGHT]<sctipt src=""http://www.etoow.com/upload/sss.jpg"" src=http://www.etoow.com/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
- 上一篇: 系统时间格式问题而导致BLOG不正常的解决方法
- 下一篇: ASP压缩数据库
-= 资 源 教 程 =-
文 章 搜 索