15分钟学会vbscript中的正则表达式 |
本文标签:vbs,正则表达式 vbs中的正则表达式 假定要搜索的字符串是 str="hello world Hello World" 1--规则基本与dos中的findstr类似 。有细微的差别 。如果对规则有困惑的,可以在cmd中看看findstr的说明就可以了 。 b--类的属性 c--类的方法 set matches=reg.execute(str)---方法execute创建由匹配项组成的集合对象 。 for each i in matches wscript.echo i.firstindex,i.value next 最后把上面的和在一起就得到一个完整的程序如下: set reg=new regexp str="hello world Hello World" reg.pattern="hello" reg.ignorecase=true reg.global=true set matches=reg.execute(str) regstr=reg.replace(str,"x") wscript.echo regstr for each i in matches wscript.echo i.firstindex,i.value '‘'‘'value可以不要 ,直接写成 i next ''''for语句也可以用下面的代码 ''''for i =0 to matches.count-1 '''''' wscript.echo i ,matches(i) '''next 正则表达式看过去看过来,还是一个糊涂 。 ''''************正则表达式练习小程序 作者 myzam 2011-2-26******* '''''特别说明:只能在cmd中运行,否则报错 。 '''''运行语法:“cscript+脚本” 。 '''''又注,vbs中\b,和dos中的\<,\>相当,表示一个单词 ''''(如word,ath,中国,0852等)的起点和终点 。 '''''这是全局设置的正则表达式,我用x作为替代了 。 set oreg=new regexp wscript.echo "请输入字符串:" str=wscript.stdin.readline wscript.echo "请输入正则表达式:" oreg.pattern=wscript.stdin.readline oreg.global=true '这里设置的是全局属性 set matches=oreg.execute(str) wscript.echo oreg.replace(str,"x") for matche=o to matches.count-1 wscript.echo "index= "&matche,"-------value= "&matches(matche) next ''''''''======================================== '''附测试题 '''' 字符串为: the thecome comethecome '''' 模板为:the '''''=========================================== 这篇文章就介绍到这,希望大家以后多多脚本之家 。 |