shine_cn

Your heart is free, so have the courage to follow it.
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

邮箱&电话号码正则表达式

Posted on 2012-08-03 14:03  shine_cn  阅读(427)  评论(0)    收藏  举报
 
 
Java代码 复制代码 收藏代码
  1. "^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$"  
  2.   
  3. 这个是一个企业级的程序里copy出来的。  

Java代码 复制代码 收藏代码
  1.   
  2.   
  3. 合法E-mail地址:    
  4. 1. 必须包含一个并且只有一个符号“@”    
  5. 2. 第一个字符不得是“@”或者“.”    
  6. 3. 不允许出现“@.”或者.@    
  7. 4. 结尾不得是字符“@”或者“.”    
  8. 5. 允许“@”前的字符中出现“+”    
  9. 6. 不允许“+”在最前面,或者“+@”    
  10.   
  11. 正则表达式如下:    
  12. -----------------------------------------------------------------------    
  13. ^(\w+((-\w+)|(\.\w+))*)\+\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$    
  14. -----------------------------------------------------------------------    
  15.   
  16. 字符描述:    
  17. ^ :匹配输入的开始位置。    
  18. \:将下一个字符标记为特殊字符或字面值。    
  19. * :匹配前一个字符零次或几次。    
  20. + :匹配前一个字符一次或多次。    
  21. (pattern) 与模式匹配并记住匹配。    
  22. x|y:匹配 x 或 y。    
  23. [a-z] :表示某个范围内的字符。与指定区间内的任何字符匹配。    
  24. \w :与任何单词字符匹配,包括下划线。    
  25. $ :匹配输入的结尾。   
  26.   
  27. 参考资料:http://www.1-100.org/asp/2006/10273.htm  



Java代码 复制代码 收藏代码
  1.   
  2.   
  3.   
  4. import java.util.regex.Matcher;   
  5. import java.util.regex.Pattern;   
  6.   
  7. /**  
  8.  * <p>  
  9.  *  
  10.  * <p>Copyright the original author or authors.  
  11.  *  
  12.  * @author Liu Huibin  
  13.  * @date Aug 27, 2010  
  14.  * @dateLastModified Aug 27, 2010  
  15.  */  
  16. public class Test {   
  17. public static void main(String[] args) {   
  18.   
  19. //电子邮件   
  20.  String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";   
  21.  Pattern regex = Pattern.compile(check);   
  22.  Matcher matcher = regex.matcher("dffdfdf@qq.com");   
  23.  boolean isMatched = matcher.matches();   
  24.  System.out.println(isMatched);   
  25.   
  26.     
  27.   
  28. /* 电话号码  
  29.  
  30. String check = "^(13[4,5,6,7,8,9]|15[0,8,9,1,7]|188|187)\\d{8}$";  
  31.  Pattern regex = Pattern.compile(check);  
  32.  Matcher matcher = regex.matcher("13555655606");  
  33.  boolean isMatched = matcher.matches();  
  34.  System.out.println(isMatched);  
  35.  
  36. */  
  37. }   
  38. }   
  39. 如何在插入数据库后返回增加的唯一ID值 | encodeURIComponent编码后java后台的解码