<html> <head> <title>正则表达式</title> <style> div{margin:15px;padding:10px;border:1px yellow solid;} </style> <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script> <script type="text/javascript"> $(function(){ $("div").click(function(){ var ta=$(this).html(); $("#regTest").val(ta.split("=")[1]); $("#strTest").val(ta.split("=")[2]); testReg(); }); // $("#strTest").onBlur(function(){ testReg();}); }); function testReg(){ var regTest=$("#regTest").val(); var regg=eval(regTest); var strTest=$("#strTest").val(); if(regg.test(strTest)){$("#spanTest").html("T");} else{$("#spanTest").html("<font color=false>F</font>");} } function _b() { if(event.keyCode ==13) testReg(); } </script> </head> <body onkeydown = "_b()"> <span style="margin:15px"><font color=red>ps:点击黄色框,自动验证里面内容。按Enter,相当于点击test按钮。。。</font></span><br><br> <span style="margin:15px"> reg:<input type="text" id="regTest" value="/^d*$/" style="300px" /> String:<input type="text" id="strTest" value="123" onKeyUp="this.value=this.value.replace(/D/g,'')" onblur="alert(event.type);" /> <input type="button" value="test" onclick="testReg()"/> <span id="spanTest" style="margin-left:10px"><font color=green>result...</font></span></span><br> <div>手机号=/^(+?86)?1[3|4|5|8]d{5,9}$/=+8613222398877</div> <div>座机传真=/^([0-9]{2,3}-?)?[0-9]{7,8}$/=021-8478998</div> <div>邮箱=/^[w-]+(.[w-]+)*@[w-]+(.[w-]+)+$/=1.1.1@1.1.1.1</div> <div>姓名=/^[u4e00-u9fa5]{2,4}$/=欧阳震华</div> <div>银行账号=/^[0-9]{16}([0-9]{3})?$/=1412585698745632145</div> <div>邮编=/^[1-9]d{5}$/=226676</div> <div>密码(四类必有,8位以上)=/^(?=^.{8,}$)(?=.*d)(?=.*W+)(?=.*[A-Z])(?=.*[a-z])(?!.* ).*$/=12Pp-slx23</div> <div>身份证号码=/^d{15}(dd[0-9xX])?$/=321012452145214569</div> <div>QQ号=/^[1-9]d{4,}$/=125563599</div> <div>IP地址=/^((2[0-4]d|25[0-5]|[01]?dd?).){3}(2[0-4]d|25[0-5]|[01]?dd?)$/=22.22.22.22</div> <div>小数=/^(-?[1-9][0-9]+)(.[0-9]+)?$/=-20.5</div> <div>整数=/^-?[1-9][0-9]*$|^0$/=33</div> <div>头尾空字符串=/(^s*)|(s*$)/g= df </div> <div>网址URL=/^(([a-zA-Z]+://)|(http://)?)[^s]*$/=www.baidu.com</div> <div>日期(年-月-日)=/(d{4}|d{2})-((0?([1-9]))|(1[1|2]))-((0?[1-9])|([12]([1-9]))|(3[0|1]))/=2002-2-3</div> <div>中文及全角标点符号(字符)=/[u3000-u301eufe10-ufe19ufe30-ufe44ufe50-ufe6buff01-uffee]/=-</div> 1 符号优先级 <font color=green>></font> (),(?:),(?=),[] <font color=green>></font> *,+,?,{n},{n,},{n,m} <font color=green>></font> ^,$,anymetacharacter <font color=green>></font> |<br> 2 x|y|z ,[xyz] ,[^xyz] ,[^a-z0-9.A-Z] ,S =[^f v] ,k=k.replace(/n/g,"o") ,onkeyup ,onkeydown </body> </html>
1 public static void main(String[] args) { 2 String mo="132223955113"; 3 String regMo="[1]([3|4|5|8][0-9]{1})[0-9]{8}"; 4 Pattern p=Pattern.compile(regMo); 5 Matcher m=p.matcher(mo); 6 if(m.find()){//包含匹配 7 System.out.println("find"+m.group(0)+" "+m.group(1)); //find:13222395511 32 8 } 9 if(m.matches()){//全部匹配 10 System.out.println("match"); 11 } 12 }
matcher.group 0表示全部匹配数据 1表示第一个()中的匹配数据
rt.jar