正则表达式结合数组提取文章中的文件名
来源:dc9.cn 发布时间:2007-10-12 13:59:00
标签:正则,RegExp,文件,数组
今天编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
出处:http://www.dc9.cn/post/279.html
0
投稿
猜你喜欢
- 本文实例讲述了Django框架文件上传与自定义图片上传路径、上传文件名操作。分享给大家供大家参考,具体如下:文件上传1、创建上传文件夹在st
- 一、题目描述求解用户登陆信息表中,每个用户连续登陆平台的天数,连续登陆基础为汇总日期必须登陆,表中每天只有一条用户登陆数据(计算中不涉及天内
- 今天学习下Go语言如何集成Gin框架编写Restful Web API的基本操作。Gin框架简化了Go原生语言构建Web应用程序的复杂度,在
- 变量插入字符串的方法Python中的format()函数是一种将变量插入字符串的方法,能够使字符串更易于阅读和理解。它支持许多不同的用法,以
- 下面是ThoughtBot 的Git使用规范流程。我从中学到了很多,推荐你也这样使用Git。第一步:新建分支首先,每次开发新功能,都应该新建
- 上篇关于爬虫的文章,我们讲解了如何运用Python的requests及BeautifuiSoup模块来完成静态网页的爬取,总结过程,网页爬虫
- Java 正则表达式正则表达式定义了字符串的模式。正则表达式可以用来搜索、编辑或处理文本。正则表达式并不仅限于某一种语言,但是在每种语言中有
- Mysqli是php5之后才有的功能,没有开启扩展的朋友可以打开您的php.ini的配置文件。 查找下面的语句:;extension=php
- 关联2张表时出现了无法创建外键的情况,从这个博客看到,问题出在第六点的Charset和Collate选项在表级和字段级上的一致性上。我的2张
- MS SQL Server 2000通过企业管理器设置数据库的定期自动备份计划。1、打开企业管理器,双击打开你的服务器2、然后点上面菜单中的
- 网上下载的 pdf 学习资料有一些会带有水印,非常影响阅读。比如下面的图片就是在 pdf 文件上截取出来的。安装模块PIL:Python I
- 文件操作我们可以使用python来操作文件,比如读取文件内容、写入新的内容等,因为任何计算机文件的本质都是一些有不同后缀的字符组成的。pyt
- 十六进制(Hexadecimal)是计算机中数据的一种表示方法。同日常生活中的表示法不一样,它由0-9,A-F组成,字母不区分大小写。与10
- 当鼠标滑过一个图片时,图片会变成另外一张图片的效果是怎么制作出来的呢?相对一些不熟悉HTML代码的朋友来说,可以使用网页编辑软件自带的动作来
- 工具版本python版本:3.8 django版本:2.0.0 mysql版本: 5.5.53 pip3创建工程djangostartDja
- 本文实例讲述了Python使用matplotlib实现在坐标系中画一个矩形的方法。分享给大家供大家参考。具体实现方法如下:import ma
- 这三种情况下所得到的server.MapPath是一致的,这就导致上传之后写入数据库的图片地址和实际图片存储地址不一致,因此,我们需要自定义
- 1,exists和in的理解(参考https://www.jb51.net/article/28922.htm) exists:如果子查询中
- python怎么模拟点击网页按钮前提环境: Python3 和 Visual Studio Code安装完毕安装selenium : 在终端
- // 涉及命名空间 using System; using System.Collections; using System.Compone