这两天,开始学习正则表达式,并且开始练习了.
首先,我没有下载正则表达式测试器的程序,因为我使用的是一个网页测试器,它的代码来自于:
正则表达式练习器 http://www.codefans.com/ArticleView/Article_5600.html
打开上面的页面,把Html代码复制下来,保存为一个htm文件就可以测试了。
如果你不想看上面的那个页面,我就把我按照以上步骤得到的页面代码贴在下面:
<HTML> <HEAD> <TITLE>正则表达式练习器</TITLE> <meta name = 安徽 池州 统计局 徐祖宁 e-mail:czjsz@stats.gov.cn> <script language="JavaScript"> function OnMove() { window.status = "("+window.event.clientX+","+window.event.clientY+")" + " :: "+document.location } </script>
<SCRIPT LANGUAGE="JavaScript1.2"> var re = new RegExp() //建立正则表达式对象 var nextpoint = 0 //匹配时的偏移量 //设置正则表达式 function setPattern(form) { var mode if(form.chkmode.checked) mode = "gi" //i:不分大小写 g:全局,好象没什么作用 else mode = "g" re.compile(form.regexp.value,mode) nextpoint = 0 form.reglist.value = "" } //检查是否有匹配 function findIt(form) { setPattern(form) var input = form.main.value if (input.search(re) != -1) { form.output[0].checked = true } else { form.output[1].checked = true } } //检查匹配位置 function locateIt(form) { setPattern(form) var input = form.main.value form.offset.value = input.search(re) } //检查所有的匹配情况 function execIt(form) { var mode if(form.chkmode.checked) mode = "gi" else mode = "g" var regexpArray = form.regexp.value.split("/r/n") //获取正则表达式到数组
if(nextpoint == 0) form.reglist.value = "" var key = true if(form.scankmode.checked) key = false else nextpoint = 0 do { var offs = 9999999999 var pos = -1 var input = form.main.value.substr(nextpoint) //对每个正则表达式进行匹配 for(var i=0;i<regexpArray.length;i++) { re.compile(regexpArray[i],mode) var matchArray = re.exec(input) if(matchArray) { if(offs > matchArray.index) { offs = matchArray.index pos = i //保存距离起始位子最近的匹配 } } } if(pos>=0) { re.compile(regexpArray[pos],mode) var matchArray = re.exec(input) for(var i=1;i<matchArray.length;i++) matchArray[i] = "$"+i+":"+matchArray[i] form.reglist.value = "["+(pos+1)+"]"+(nextpoint+matchArray.index)+" => " + matchArray[0] +"/n"+form.reglist.value form.matchlist.value = "$0:"+matchArray.join("/n") nextpoint = nextpoint + matchArray.index + matchArray[0].length }else { if(!key) form.reglist.value = "没有找到/n" + form.reglist.value form.matchlist.value = " " nextpoint = 0 key = false } }while(key) }
//设置当前使用的正则表达式 function setregexp(n) { var s = document.all.regexplist.value.split("/r/n") document.all.regexp.value = s[n*2-1] //.replace("/r","") nextpoint = 0 }
//定义选择监视 var isNav = (navigator.appName == "Netscape") function showSelection() { if (isNav) { var theText = document.getSelection() } else { var theText = document.selection.createRange().text } if(theText.length>0 && document.all.selechkmode.checked) document.all.regexp.value = theText } if (isNav) { document.captureEvents(Event.MOUSEUP) } document.onmouseup = showSelection </SCRIPT>
</HEAD> <BODY style="font-size=9pt;" OnMouseMove=OnMove()> <FORM><table width=100% cellspacing=0 cellpadding=0><tr><td><font color=red>正规表达式练习器</font></td><td align=right><a href=mailto:czjsz_ah@stats.gov.cn>czjsz_ah@stats.gov.cn</a></td></tr></table> <table width=100% broder=1 frame=above rules=none style="font-size:9pt;"> <tr><td width=50% valign=top> 输入一些被寻找的正文:<BR> <TEXTAREA NAME="main" COLS=58 ROWS=5 WRAP="virtual" style="font-size:9pt;"> 09-11-2001 09/11/2001 czjsz_ah@stats.gov.cn asdff 12345 196.168.1.3 www.sohu.com ftp://www.chinaasp.com 2001.9.11 http://www.active.com.cn/club/bbs/bbsView.asp http://www.163.com/inden.htm </TEXTAREA><BR> 进行匹配的正规表达式: 忽略大小写<INPUT TYPE="checkbox" NAME="chkmode" checked style="font-size:8pt;height:18px"><BR> <TEXTAREA NAME="regexp" COLS=51 ROWS=5 style="font-size:9pt;"></TEXTAREA> <INPUT TYPE="button" VALUE="清除" onClick="this.form.regexp.value=''" style="font-size:8pt;height:18px"><BR> <INPUT TYPE="button" VALUE="能找到吗?[regexObject.test(string)]" style="font-size:8pt;width:70%;height:18px" onClick="findIt(this.form)"> <INPUT TYPE="radio" NAME="output" style="font-size:8pt;height:18px">Yes <INPUT TYPE="radio" NAME="output" style="font-size:8pt;height:18px">No <BR> <INPUT TYPE="button" VALUE="在哪里?[string.search(regexObject)]" style="font-size:8pt;width:70%;height:18px" onClick="locateIt(this.form)"> <INPUT TYPE="text" NAME="offset" SIZE=4 style="font-size:8pt;height:18px"> </td> <td valign=top> 测试用正则表达式列表: 使用第<input type=text name=num size=2 value=1 style="font-size:8pt;height:18px">个<input type=button value=Go onClick=setregexp(this.form.num.value) style="font-size:8pt;height:18px"> 允许复制<INPUT TYPE="checkbox" NAME="selechkmode" style="font-size:8pt;height:18px"> <textarea NAME="regexplist" cols=58 rows=14 wrap=off style="font-size:9pt;"> 1.检查日期: (1[0-2]|0?[1-9])[-./](0?[1-9]|[12][0-9]|3[01])[-./](/d/d/d/d)) 2.检查数字: ([-+]?[0-9]+/.?[0-9]+) 3.检查URL: ((http|ftp)://)?(((([/d]+/.)+){3}[/d]+(/[/w./]+)?)|([a-z]/w*((/./w+)+){2,})([/][/w.~]*)*) 4.检查E-mail /w+@((/w+[.]?)+) </textarea> </td></tr> <tr><td valign=bottom> <INPUT TYPE="button" VALUE="有哪些?[regexObject.exec(string)]" style="font-size:8pt;width:70%;height:18px" onClick="execIt(this.form)"> 单步<INPUT TYPE="checkbox" NAME="scankmode" style="font-size:8pt;height:18px"><BR> <TEXTAREA NAME="reglist" COLS=58 ROWS=8 style="font-size:9pt;"></TEXTAREA> </td> <td valign=bottom> 匹配到的成分:(单步时可见) <TEXTAREA NAME="matchlist" COLS=58 ROWS=8 style="font-size:9pt;"></TEXTAREA> </td></tr></table></FORM> <script> setregexp(1) </script> </BODY> </HTML>
|
|
然后,打开这个网页,开始学习和练习测试。
我测试的代码是:利用百度查找关键字:正则表达式 之后得到的页面源文件。
我根据我当时的目的写出了下面的测试表达式。
测试数字。/d ,测试的是0-9的单个数字.例如 /d/d 可以匹配11,12,78,但是不能够匹配3a,9d等
表达式我用的是 /d 匹配成功,但是查看有哪些的时候,这个网页突然死了,5555555~
regexes= /d/d ,匹配连着的两位数.可以找到很多,比如10,20,31,30,49...
refexes= d/d ,由于选中了忽略大小写,所以匹配到d3,D9等等很多.
我想,如果我要匹配所有的数据(数字的),如12,123,2344555,1000等等,难道要/d/d/d/d.....下去?况且,我又不知道数据有多少位,所以我就测试了一下 regexes= /d+/d 又很多:...14 10900 68 797 721...,看来我的目的达到了.
高级:查了一下书,知道 + 号可以用来查找重复字符,它的含义是匹配前面的字符一次或多次,注意,至少要匹配一次.比如z+可以匹配z,zz,zzz,zzzz,zzzzz........
可见, /d+/d 可以匹配任意位数字再加末尾一个数字(就是两位数和两位数以上),它和 /d/d+ 是一样的效果
而 /d+ 就可以匹配一位数和以上,即所有数字
提示: /d/d 匹配两位数 /d/d/d 匹配三位数 /d/d/d/d 匹配四位数........
*我想找3开头的三位数,怎么办?查书就知道是这样的:
匹配 3/d/d
一点结果:
[1]22109 => 301
[1]21485 => 312
[1]21051 => 312
要是我想匹配首位数字是1,2,5中的一个的三位数,又该怎么办?查书呗: 匹配 [125]/d/d
一点结果:
[1]22111 => 198
[1]21966 => 200
[1]21888 => 100
[1]21484 => 231
提示: [...]匹配括号中的任一个字符,注意,只匹配一个,比如[123]匹配1,2,3中的任何一个
[12adfu 67g]匹配1,2,a,d,f,u, (这个是空格),6,7,g中任一个
[-=/';]匹配-,=,/,',;中的任一个
[0-9]匹配0至9的任意一个字符 [a-z]匹配a至z的任一个字符 [A-Z]匹配A至Z任一个字符
[0-9a-z]匹配0至9和a至z中的任一个字符
如果忽略大小写,[a-z]或者[a-Z]匹配a-z和A-Z中的任一个字符
[0-9a-zA-Z_]匹配任一个单词字符,也就是匹配0-9,a-z,A-Z和下划线字符.比如[0-9a-zA-Z_][0-9a-zA-Z_][0-9a-zA-Z_]可以匹配Acb,ht5,h_Y,但是不匹配h@7,j#8,$%^,等等
另外, /w 和[0-9a-zA-Z_] 是等价的. 比如 /w/w/w/w 可以匹配hU_8,ghua,但是不匹配^&*(,fd_%,lkj_@等等
最后,[^...]匹配不是括号中的任一个字符,比如[^abh5]匹配除了a,b,h,5之外的所有字符,[^a-z]可以匹配除了小写字符a-z之外的字符,如大写字母A-Z,数字0-9,特殊字符$%^&*)(@等等.