今天编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=””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=””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