用例子来学习正则表达式

 
 
  1. import java.util.regex.Matcher; 
  2. import java.util.regex.Pattern; 
  3.  
  4.  
  5.  
  6. /** 
  7.  * 正则表达式:字符串的处理利器 
  8.  * @author kenan 
  9.  * 
  10.  */ 
  11. public class RegularExpressions { 
  12.      
  13.     public static void main(String[] args) { 
  14.         // 在正则表达式中 . 代表一个字符 
  15. //      System.out.println("abc".matches("...")); 
  16.         // \d 代表一个数字:使用表达式进行匹配替换 
  17. //      System.out.println("ab12356c".replaceAll("\\d", "-")); 
  18.         //这个是我们平时的用法 
  19. //      System.out.println("ab12346c".replaceAll("123", "---")); 
  20.          
  21.         //编译一个正则表达式 
  22. //      Pattern p = Pattern.compile("[a-z]{3}"); 
  23.         // 匹配一个字符串  , 返回一个 Matcher 对象 
  24. //      Matcher m = p.matcher("abc"); 
  25. //      System.out.println(m.matches()); 
  26.          
  27.         // . : 一个字符  
  28.         //* : 0或多个字符  
  29.         //+: 1或者多个 
  30.         //?:0 或者 1个 
  31. //      System.out.println("a".matches(".")); 
  32. //      System.out.println("aa".matches("aa")); 
  33. //      System.out.println("aaaa".matches("a*")); 
  34. //      System.out.println("".matches("a*")); 
  35. //      System.out.println("aaa".matches("a?")); 
  36. //      System.out.println("".matches("a?")); 
  37. //      System.out.println("a".matches("a?")); 
  38. //      System.out.println("123456".matches("\\d{3,100}")); 
  39. //      System.out.println("12".matches("\\d{3,100}")); 
  40. //      System.out.println("192.168.0.aaa".matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}")); 
  41. //      System.out.println("192.168.0.2".matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}")); 
  42. //      System.out.println("192".matches("[0-2][0-9][0-9]")); 
  43. //      System.out.println("192".matches("[0-9]")); 
  44.          
  45.         //范围匹配 
  46. //      System.out.println("a".matches("[abc]")); 
  47. //      System.out.println("a".matches("[^abc]")); 
  48. //      System.out.println("A".matches("[a-zA-Z1-9]")); 
  49. //      System.out.println("3".matches("[a-zA-Z1-9]")); 
  50. //      System.out.println("A".matches("[a-z]|[A-Z]")); 
  51. //      System.out.println("R".matches("[A-Z&&[RFG]]")); 
  52.          
  53.          
  54.         //  认识  \s \w  \d \ 
  55.         // \d 一个数字 [0-9] 
  56.         // \D 上面取反 
  57.         // \s 一个空字符 [ \t\n\r\f\x0B] 
  58.         // \S 上面取反 
  59.         // \w 一个字符[a-zA-Z_0-9] 
  60.         // \W 出了上面的字符以为的其他字符 
  61.         // \\  代表 一个 \ 
  62. //      System.out.println(" \n\r\t".matches("\\s{1,4}")); 
  63. //      System.out.println(" ".matches("\\S")); 
  64. //      System.out.println("a_8".matches("\\w{1,3}")); 
  65. //      System.out.println("abc888&^%".matches("[a-z]{1,3}\\d+[&^#%]+")); 
  66. //      System.out.println("\\".matches("\\\\")); 
  67.          
  68.          
  69.         // Posix Style 
  70. //      System.out.println("a".matches("\\p{Lower}")); 
  71.          
  72.         //边界处理 
  73.         // ^ 在[ ] 里代表取反 在外面代表字符开头 
  74.         // $ 代表以什么结尾 
  75.         // \b代表 单词结束 
  76. //      System.out.println("hello sir".matches("^h.*")); 
  77. //      System.out.println("hello sir".matches(".*ir$")); 
  78. //      System.out.println("hello sirr".matches(".*ir$")); 
  79. //      System.out.println("hello sir".matches("^h[a-z]{1,3}o\\b.*")); 
  80. //      System.out.println("hellosir".matches("^h[a-z]{1,3}o\\b.*")); 
  81. //      System.out.println("hello         sir".matches("^h[a-z]{1,3}o\\b.*")); 
  82. //      System.out.println("hello&         sir".matches("^h[a-z]{1,3}o\\b.*")); 
  83. //      System.out.println("hello.         sir".matches("^h[a-z]{1,3}o\\b.*")); 
  84.          
  85.         //找出空白行 
  86. //      System.out.println(" \n".matches("^[\\s&&[^\\n]]*\\n$")); 
  87. //      System.out.println("\n".matches("^[\\s&&[^\\n]]*\\n$")); 
  88.          
  89.         // email 
  90. //      System.out.println("soukenan@qq.com".matches("[\\w.-]+@[\\w.-]+\\.[\\w]+")); 
  91.          
  92.         // matches from lookingat 
  93.          
  94. //      Pattern p = Pattern.compile("\\d{3,5}"); 
  95. //      String s = "123-45674-234-00"; 
  96. //      Matcher m = p.matcher(s); 
  97. //      System.out.println(m.matches()); //false  //从前往后匹配 
  98. //      m.reset(); // 把光标重置 
  99. //      System.out.println(m.find());//true  //从当前的标志开始查找 
  100. //      System.out.println(m.start()+":"+m.end());  
  101. //      System.out.println(m.find()); //true 查找匹配的字串 
  102. //      System.out.println(m.start()+":"+m.end()); 
  103. //      System.out.println(m.find()); //true 
  104. //      System.out.println(m.start()+":"+m.end()); 
  105. //      System.out.println(m.find());//false 
  106. //       
  107. //      System.out.println(m.lookingAt()); //true  //从头开始看看 是否匹配 
  108. //      System.out.println(m.lookingAt()); //true 
  109. //      System.out.println(m.lookingAt()); //true 
  110. //      System.out.println(m.lookingAt()); //true 
  111.          
  112.          
  113.         //replacement 字符串的替换 
  114. //      Pattern p = null;  
  115. //      p = Pattern.compile("java"); 
  116. //      p = Pattern.compile("java", Pattern.CASE_INSENSITIVE); //忽略大小写 
  117. //      Matcher m = p.matcher("java Java JaVa javA ,i love JAVA!"); 
  118. //      while(m.find()){ 
  119. //          System.out.println(m.group()); 
  120. //      } 
  121. //      System.out.println(m.replaceAll("JAVA")); //把找到的匹配字符串转换成JAVA 
  122. //      StringBuffer buf = new StringBuffer(); 
  123. //      int i =0; 
  124. //      while(m.find()){ 
  125. //          i++; 
  126. //          if(i%2==0){ 
  127. //              m.appendReplacement(buf, "java");  //偶数 转换成 java 
  128. //          }else{ 
  129. //              m.appendReplacement(buf, "JAVA"); //奇数转换成 JAVA 
  130. //          } 
  131. //      } 
  132. //      m.appendTail(buf);//把尾部字符串添加上去 
  133. //      System.out.println(buf); 
  134.          
  135.         //group 分组 
  136.         //()  这个是用来分组的 
  137.         Pattern p = Pattern.compile("(\\d{3,5})([a-z]{2})"); 
  138.         String s = "123aa-3456bb-234cc-00"
  139.         Matcher m = p.matcher(s); 
  140.         while(m.find()){ 
  141.             System.out.println(m.group(1)+":"+m.group(2)); 
  142.         } 
  143.          
  144.          
  145.     }  
  146.      

 

本文出自 “Kenan_ITBlog” 博客,请务必保留此出处http://soukenan.blog.51cto.com/5130995/1131428

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值