欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

文件名 正则表达式提取方法

程序员文章站 2022-06-05 09:12:09
下面这段子程基本上可以算是比较不错的通用匹配了。(ps:我突然发现code_lite把我的ubb转义了!!!晕,我只好自己转义了。。。) dim objregex...
下面这段子程基本上可以算是比较不错的通用匹配了。(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

ps:最近正则又精进不少...