A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 战狼 中级黑马   /  2014-8-1 13:08  /  667 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. <FONT face=宋体>public class MatchesDemo {
  2. public static void main(String[] args) {
  3. String[] str = {
  4. "penglei@qq.com",
  5. "penglei@crizyit.org",
  6. "penglei@sohu.com",
  7. "penglei@abc.xx",};
  8. String maileRe = "\\w{3,20}@\\w+\\.(com|org|cn|net|gov)";//{3,20}表示字符最小不少于3个,最大不少于20个
  9. Pattern pa = Pattern.compile(maileRe);
  10. Matcher m = null;
  11. try {
  12. for (String mail : str) {
  13. if (m==null) {
  14. m = pa.matcher(mail);
  15. }else
  16. {
  17. m.reset(mail);
  18. }
  19. String result = mail+(m.matches() ? "是":"不是")+"一个有效地址";
  20. System.out.println(result);
  21. }
  22. } catch (Exception e) {
  23. // TODO: handle exception
  24. }

  25. String[] str1 = {
  26. "123456789",
  27. "135559",
  28. "555556987",
  29. "18982774361",
  30. };
  31. String mm = "^([18[09]])\\d{8}$";
  32. Pattern pop = Pattern.compile(mm);
  33. Matcher ma = null;
  34. for (String num : str1) {
  35. if (ma==null) {
  36. ma = pop.matcher(num);
  37. }else
  38. {
  39. m.reset(num);
  40. }
  41. String result = num + (ma.matches() ? "是" : "不是") +"一个有效电话";
  42. System.out.println(result);
  43. }
  44. }
  45. }</FONT>
复制代码
为什么我匹配电话号码始终不对

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马