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

时间:2021-05-18

下面这段子程基本上可以算是比较不错的通用匹配了。(PS:我突然发现CODE_LITE把我的UBB转义了!!!晕,我只好自己转义了。。。)

DimobjRegExp,Matches,i,DC9_DOT_CN_MATCH
DimaryMatch()
RedimPreservearyMatch(0)
SetobjRegExp=NewRegExp
objRegExp.IgnoreCase=True
objRegExp.Global=True
objRegExp.Pattern="(?:\[[^\]]+\]([^\[]+)\[\/[^\]]+\])|(?:(?:href|src)=([^\s|^>]+)[""|>|\s\'])"
SetMatches=objRegExp.Execute("[img]2312.jpg[/img][img]2312.jpg[/img]hh[img]http://www.dc9.cn/cm/upload/2ed312.jpg[/img]<ahref=""http://www.dc9.cn/ddd.jpg""></a>href=""http://www.dc9.cn/dccdd.jpg"">[IMG_RIGHT=400,300,title]upload/2assas.jpg[/IMG_RIGHT]<sctiptsrc=""http://www.dc9.cn/upload/sss.jpg""")
DimTmpMatch
Fori=0toMatches.Count-1
CallInsertDataToArray(Matches(i).SubMatches(0),aryMatch)
CallInsertDataToArray(Matches(i).SubMatches(1),aryMatch)
Next

Fori=0toUbound(aryMatch)
IfNotIsNull(aryMatch(i))AndTrim(aryMatch(i))<>""ThenResponse.writearyMatch(i)&"<br>"
Next

FunctionInsertDataToArray(Data,ByRefaryMatch)
IfTrim(Data)<>""Then
Data=Replace(Data,"'","")
Data=Replace(Data,"""","")
Data=Replace(Data,"\","/")
Data=Split(Data,"/")(Ubound(Split(Data,"/")))
RedimPreservearyMatch(Ubound(aryMatch)+1)
DC9_DOT_CN_MATCH=False
Forj=0toUbound(aryMatch)
IfaryMatch(j)=DataThenDC9_DOT_CN_MATCH=True
Next
IfNotDC9_DOT_CN_MATCHThenaryMatch(Ubound(aryMatch))=Data
EndIf
EndFunction
实际上,Z-Blog的话就匹配Upload就可以了(但是我为了省事,也为了更精确些,就干脆在上面的那个匹配上面加上了upload,所以看上去好像下面这个更复杂些,其实理论上应该下面这个简单些,毕竟有upload管着呢。。),也不用SPLit那个/.这么写就可以了:

DimobjRegExp,Matches,i,ZC_UPLOAD_MATCH
DimaryMatch()
RedimPreservearyMatch(0)
SetobjRegExp=NewRegExp
objRegExp.IgnoreCase=True
objRegExp.Global=True
objRegExp.Pattern="(?:\[[^\]]+\][^\[]*upload\/([^\[|^\\|^\/]+)\[\/[^\]]+\])|(?:(?:href|src)=""{0,1}[^\s|^""|^>|^']*upload\/([^\s|^>]+)[""|>|\s\'])"
SetMatches=objRegExp.Execute("[img]2312.jpg[/img][img]2312.jpg[/img]hh[img]http://www.dc9.cn/cm/upload/2ed312.jpg[/img]<ahref=""http://www.dc9.cn/ddd.jpg""></a>href=""http://www.dc9.cn/dccdd.jpg"">[IMG_RIGHT=400,300,title]upload/2assas.jpg[/IMG_RIGHT]<sctiptsrc=""http://www.dc9.cn/upload/sss.jpg""src=http://www.dc9.cn/upload/sasds.jpg'>")
DimTmpMatch
Fori=0toMatches.Count-1
CallInsertDataToArray(Matches(i).SubMatches(0),aryMatch)
CallInsertDataToArray(Matches(i).SubMatches(1),aryMatch)
Next

Fori=0toUbound(aryMatch)
IfNotIsNull(aryMatch(i))AndTrim(aryMatch(i))<>""ThenResponse.writearyMatch(i)&"<br>"
Next

FunctionInsertDataToArray(Data,ByRefaryMatch)
IfTrim(Data)<>""Then
Data=Replace(Data,"'","")
Data=Replace(Data,"""","")
RedimPreservearyMatch(Ubound(aryMatch)+1)
ZC_UPLOAD_MATCH=False
Forj=0toUbound(aryMatch)
IfaryMatch(j)=DataThenZC_UPLOAD_MATCH=True
Next
IfNotZC_UPLOAD_MATCHThenaryMatch(Ubound(aryMatch))=Data
EndIf
EndFunction

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

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章