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

2018-09-06 11:25

阅读:497

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

DimobjRegExp,Matches,i,DC9_DOT_CN_MATCH
DimaryMatch()
RedimPreservearyMatch(0)
SetobjRegExp=NewRegExp
objRegExp.IgnoreCase=True
objRegExp.Global=True
objRegExp.Pattern=(?:\[[^\]]+\]([^\[]+)\[\/[^\]]+\])(?:(?:hrefsrc)=([^\s^>]+)[>\s\])
SetMatches=objRegExp.Execute([img]2312.jpg[/img][img]2312.jpg[/img]hh[img]
DimTmpMatch
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\/([^\[^\\^\/]+)\[\/[^\]]+\])(?:(?:hrefsrc)={0,1}[^\s^^>^]*upload\/([^\s^>]+)[>\s\])
SetMatches=objRegExp.Execute([img]2312.jpg[/img][img]2312.jpg[/img]hh[img]
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:最近正则又精进不少...


评论


亲,登录后才可以留言!