欢迎光临
我们一直在努力

正则表达式在网络编程中的运用(5)

建站超值云服务器,限时71元/月

  三、ubb正则表达模板实例

  下面是我写的一个ubb函数,这个函数基本上能使你的论坛成为一个优秀的ubb代码论坛了。当然,通过改进后,你可以得到一个更强大的ubb论坛。

function rethestr(face,str)
 dim re,str

 re="\>"
 str=checkexp(re,str,">")

 re="\<"
 str=checkexp(re,str,"<")

 re="\n\r\n/"
 str=checkexp(re,str,"<p>")

 re=chr(32)
 str=checkexp(re,str," ")

 re="\r"
 str=checkexp(re,str," ")

 re="\[img\]((http:(\/\/|\\\\)){1}((\w)+[.]){1,3}_
(net|com|cn|org|cc|tv)(((\/[\~]*|\\[\~]*)
(\w)+)|[.](\w)+)*(\w)+[.]{1}(gif|jpg|png))\[\/img\]" 查找图片地址
 str=checkexp(re,str," <img src=$1> ")

 re="\[w\](http:(\/\/|\\\\)((\w)+[.]){1,}_
(net|com|cn|org|cc|tv)(((\/[\~]*|\\[\~]*)(\w)+)|[.](\w)+)*
(((([?](\w)+){1}[=]*))*((\w)+){1}([\&](\w)+[\=](\w)+)*)*)\[\/w\]" 查找帧地址
 str=checkexp(re,str,"<iframe width=300 height=300 src=$1></iframe>")

 re="([^(>)])(<br>)*((http|https|ftp):_
(\/\/|\\\\)((\w)+[.]){1,}(net|com|cn|org|cc|tv|_
([0-9]{1,3}))(((\/[\~]*|\\[\~]*)(\w)+)|[.](\w)+)*_
(((([?](\w)+){1}[=]*))*((\w)+){1}([\&](\w)+[\=](\w)+)*)*)" 查找链接地址
 str=checkexp(re,str,"$1$2 <a href=$3 target=_blank>$3</a> ")
 re="([^(http://|http:\\)])((www|cn)[.](\w)+[.]{1,}_
(net|com|cn|org|cc)(((\/[\~]*|\\[\~]*)(\w)+)|[.](\w)+)*
(((([?](\w)+){1}[=]*))*((\w)+){1}([\&](\w)+[\=](\w)+)*)*)
" 查找不以http://开头的地址
 str=checkexp(re,str,"$1 <a href=http://$2 target=_blank>$2</a> ")
 re="([^(=)])((\w)+[@]{1}((\w)+[.]){1,3}(\w)+)" 查找邮件地址
 str=checkexp(re,str," <a href=mailto:$2>$2</a> ")
 re="\[color=(((\w)+)|][#][0-f]{6})\]((.)+)\[\/color\]" 替换字体色彩
 str=checkexp(re,str,"<font color=$1>$4</font>")
 re="\[size=(][0-9]{1})\]((.)+)\[\/size\]" 替换字体大小
 str=checkexp(re,str,"<font size=$1>$2</font>")
 re="\[font=((.)+){1,3}\]((.)+)\[\/font\]" 替换字体
 str=checkexp(re,str,"<font face=$1>$3</font>")
 re="(\[b\])(.+)(\[\/b\])" 加粗字体
 str=checkexp(re,str,"<b>$2</b>")
 re="(\[u\])(.+)(\[\/u\])" 下画线
 str=checkexp(re,str,"<u>$2</u>")
 re="(\[li\])(.+)(\[\/li\])" 列表
 str=checkexp(re,str,"<li>$2</li>")
 re="(\[quote\])(.+)(\[\/quote\])" 引用
 str=checkexp(re,str,"_
<blockquote>引用:<hr size=1>$2<hr size=1></blockquote>")
 re="\[email=((\w)+][@]{1}((\w)+[.]){1,3}(\w)+)\](.+)(\[\/email\])" 邮件
 str=checkexp(re,str,"<a href=mailto:$1>$6</a>")
 re="(\[center\])(.+)(\[\/center\])" 居中
 str=checkexp(re,str,"<center>$2</center>")

 re="fuck"
 str=checkexp(re,str,"***")

 re="操"
 str=checkexp(re,str,"***")

 re="sex"
 str=checkexp(re,str,"***")

 re="tmd"
 str=checkexp(re,str,"***")

 re="shit"
 str=checkexp(re,str,"***")

 rethestr=str
end function

  ubb代码如下:

[center] [/center] [email=] [/email] 正则表达式在网络编程中的运用(5)

  • [font=impact] [/font] [color=yellow] [/color] [size=4] [/size]

      测试代码如下:

    正则表达式在网络编程中的运用(5) 正则表达式在网络编程中的运用(5)http://cn.yahoo.com
    http://aol.com http://192.168.0.1
    www.yahoo.com how are you[/b]
    http://page2000.xiloo.com/~page2000?pageno=2&action=del
    lucaihui@cmmail.com 大家好http:\\page2000.shit
    <font color=red>http://test.com</font>http://test

      全部符合预想结果

      asp正则表达式对像函数如下:

      function checkexp(patrn,strng,tagstr)
     dim regex,matches

     set regex=new regexp建立一个新对像
     regex.pattern=patrn设置模板
     regex.ignorecase=true搜索是否区分大小写的 true表是不区分 flase表示区分
     regex.global=true搜索是否应用于整个字符串

     matches=regex.replace(strng,tagstr)匹配并替代字符串

     checkexp=matches返回函数结果
    end function

      把上面的两个函数存为一个页面(如ubbcode.asp),这样就组成一个完整的ubb函数了。

      在你的论坛上加上这个函数,就成了支持ubb代码的论坛了。只要在用时调用这个函数就行了。调用形式如

       text=rethestr(text)

      如果你要进一步测试,可以到http://www.4u2v.com/bbs去测试,因为该论坛就采用了此模板。

    赞(0)
    版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 正则表达式在网络编程中的运用(5)
    分享到: 更多 (0)

    相关推荐

    • 暂无文章