zheze2

浅谈Java正则表达式中的Pattern类与Matcher类

计算机等级考试   辅导课程   2009-06-25  【字体:大 中 小 】  JavaEye    我要评论( <script src="http://www.gzu521.com/e/public/ViewClick?classid=2915&amp;id=21053&amp;down=2" type="text/javascript"></script> 0 )
<script src="http://www.gzu521.com/d/js/acmsd/thea4.js"></script><script src="http://www.gzu521.com/recommend/gg_cms_contenttop.js"></script>
<script src="http://www.gzu521.com/d/js/acmsd/thea5.js"></script><script src="http://www.gzu521.com/recommend/gg_cms_content.js"></script><script type="text/javascript">&lt;!-- google_ad_client = &quot;pub-5138858050714057&quot;; google_ad_type = &quot;text_image&quot;; google_ad_width = 336; google_ad_height = 280; google_ad_format = &quot;336x280_as&quot;; google_ad_channel =&quot;3346635250&quot;; google_color_border = &quot;F7FDFF&quot;; google_color_bg = &quot;F7FDFF&quot;; google_color_link = &quot;000000&quot;; google_color_text = &quot;333333&quot;; google_color_url = &quot;222222&quot;; //--&gt;</script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script>

Pattern类说明

指定为字符串的Java正则表达式必须首先被编译为pattern类的实例。然后,可将得到的模式用于创建 Matcher 对象,依照Java正则表达式,该对象可以与任意字符序列匹配。执行匹配所涉及的所有状态都驻留在匹配器中,所以多个匹配器可以共享同一模式。

因此,典型的调用顺序是

 
  1. Pattern  p  =  Pattern .compile("a*b");  
  2. Matcher  m  =  p .matcher("aaaaab");  
  3. boolean  b  =  m .matches(); 

在仅使用一次正则表达式时,可以方便地通过pattern类定义 matches 方法。此方法编译表达式并在单个
调用中将输入序列与其匹配。语句
boolean b = Pattern.matches("a*b", "aaaaab");

等效于上面的三个语句,尽管对于重复的匹配而言它效率不高,因为它不允许重用已编译的模式。
此类的实例是不可变的,可供多个并发线程安全使用。Matcher 类的实例用于此目的则不安全。

Matcher类说明

通过调用模式的 matcher 方法从模式创建匹配器。创建匹配器后,可以使用它执行三种不同的匹配操作:

1  matches   方法尝试将整个输入序列与该模式匹配。

(注:当调用String的matches()方法时,实际上是调用Pattern的静态方法matches().也就是相当于调Matcher的matches(),所以是整个输入序列与模式匹配.)

2  lookingAt  尝试将输入序列从头开始与该模式匹配。

3  find     方法扫描输入序列以查找与该模式匹配的下一个子序列。 

此类的实例用于多个并发线程是不安全的。

测试代码

package test;  

 
  1. import java.util.regex.Matcher;     
  2. import java.util.regex.Pattern;     
  3. /**     
  4.  * java中运用正则表达式的两个重要类:Pattern与Matcher     
  5.  * @author fhd001     
  6.  */     
  7. public class PatternAndMatcherTest {     
  8.       public static void main(String[] args) {     
  9.         /*     
  10.          * 常用的调用     
  11.          */     
  12.         Pattern  p1  =  Pattern .compile("a*b");     
  13.         String  str1  =  "aaaab" ;     
  14.         Matcher  m1  =  p1 .matcher(str1);     
  15.         boolean  b1  =  m1 .matches();     
  16.         System.out.println(b1);     
  17.              
  18.         String  str2  =  "b" ;     
  19.         Matcher  m2  =  p1 .matcher(str2);     
  20.         boolean  b2  =  m2 .matches();     
  21.         System.out.println(b2);     
  22.           /*     
  23.          * 另一种调用     
  24.          * 等效于上面的语句,尽管对于重复的匹配而言它效率不高,因为它不允许重用已编译的模式。      
  25.          * 但它可供多个并发线程安全使用,而上面的调用则就不是安全的.     
  26.          */     
  27.         boolean  b3  =  Pattern .matches("a*b", "aaab");     
  28.         System.out.println(b3);     
  29.              
  30.         //Pattern类的pattern方法:从pattern类的实例中返回匹配模式的字符串表示     
  31.         String  pattern1  =  p1 .pattern();     
  32.         System.out.println(pattern1);     
  33.              
  34.         //Pattern类的split方法     
  35.         String[] arr1  =  p1 .split("rrrrraaabccccaaaaab");     
  36.         for (String string : arr1) {     
  37.             System.out.println(string+" > > > > ");     
  38.         }     
  39.         /*     
  40.          * Matcher类     
  41.          *      
  42.          * matches方法:       方法尝试将整个输入序列与该模式匹配     
  43.          * lookingAt方法: 尝试将输入序列从头开始与该模式匹配,与 matches 方法类似,     
  44.          *                  此方法始终从区域的开头开始;与之不同的是,它不需要匹配整个区域。      
  45.          * find方法:          方法扫描输入序列以查找与该模式匹配的下一个子序列     
  46.          */     
  47.         String  str3  =  "aabbcccaaaaaeeeaaaaaaaaagggga" ;     
  48.         Pattern  p3  =  Pattern .compile("a+");     
  49.         Matcher  m3  =  p3 .matcher(str3);     
  50.         boolean  bo4  =  m3 .matches();     
  51.         System.out.println("matches方法:  "+bo4);     
  52.         /*     
  53.          * lookingAt方法,从开头第一个字符进行匹配,匹配成功了不再继续匹配,     
  54.          * 从第一个字符开始,匹配失败了,也不继续匹配.不需要匹配整个序列     
  55.          */     
  56.         boolean  bo5  =  m3 .lookingAt();     
  57.         if(bo5){     
  58.             //group方法(不带参数)返回的就是匹配的子字符串.     
  59.             System.out.println("lookingAt方法:  "+m3.group());     
  60.         }     
  61.      //find方法:找到一个匹配的子串,还会继续找下一个子串.     
  62.         while(m3.find()){     
  63.             System.out.println("find方法:  "+m3.group());     
  64.         }     
  65.  /*     
  66.          * 带参数的group方法与不带参数的group方法区别     
  67.          * 不带参数的group方法:find方法与lookingAt方法匹配出来的子序列(上面有演示)     
  68.          * 带参数的group方法: 返回在以前匹配操作期间由给定组捕获的输入子序列。     
  69.          */     
  70.         String  str6  =  "aaabbbccc" ;     
  71.         Pattern  p5  =  Pattern .compile("(a+)(b+)(c+)");     
  72.         Matcher  m5  =  p5 .matcher(str6);     
  73.         boolean  boo  =  m5 .matches();     
  74.         if(boo){     
  75.             int  k  =  m5 .groupCount()+1;//加1就是把0下标的整个字符序列加上,它也作为一组放在0下标的位置.     
  76.             if(k > 0){     
  77.                 for(int  i = 0 ;i                    System.out.println(m5.group(i));     
  78.                 }     
  79.             }     
  80.         }     
  81.     }     
  82. }    
  83.  
  84. package test;  
  85.  
  86. import java.util.regex.Matcher;  
  87. import java.util.regex.Pattern;  
  88.  
  89. /**  
  90.  * java中运用正则表达式的两个重要类:Pattern与Matcher  
  91.  * @author fhd001  
  92.  */  
  93. public class PatternAndMatcherTest {  
  94.  
  95.  public static void main(String[] args) {  
  96.     
  97.   /*  
  98.    * 常用的调用  
  99.    */  
  100.     
  101.   Pattern  p1  =  Pattern .compile("a*b");  
  102.     
  103.   String  str1  =  "aaaab" ;  
  104.   Matcher  m1  =  p1 .matcher(str1);  
  105.   boolean  b1  =  m1 .matches();  
  106.   System.out.println(b1);  
  107.     
  108.   String  str2  =  "b" ;  
  109.   Matcher  m2  =  p1 .matcher(str2);  
  110.   boolean  b2  =  m2 .matches();  
  111.   System.out.println(b2);  
  112.   /*  
  113.    * 另一种调用  
  114.    * 等效于上面的语句,尽管对于重复的匹配而言它效率不高,因为它不允许重用已编译的模式。   
  115.    * 但它可供多个并发线程安全使用,而上面的调用则就不是安全的.  
  116.    */  
  117.   boolean  b3  =  Pattern .matches("a*b", "aaab");  
  118.   System.out.println(b3);  
  119.     
  120.   //Pattern类的pattern方法:从pattern类的实例中返回匹配模式的字符串表示  
  121.   String  pattern1  =  p1 .pattern();  
  122.   System.out.println(pattern1);  
  123.     
  124.   //Pattern类的split方法  
  125.   String[] arr1  =  p1 .split("rrrrraaabccccaaaaab");  
  126.   for (String string : arr1) {  
  127.    System.out.println(string+" > > > > ");  
  128.   }  
  129.    /*  
  130.    * Matcher类  
  131.    *   
  132.    * matches方法:  方法尝试将整个输入序列与该模式匹配  
  133.    * lookingAt方法: 尝试将输入序列从头开始与该模式匹配,与 matches 方法类似,  
  134.    *      此方法始终从区域的开头开始;与之不同的是,它不需要匹配整个区域。   
  135.    * find方法:   方法扫描输入序列以查找与该模式匹配的下一个子序列  
  136.    */  
  137.   String  str3  =  "aabbcccaaaaaeeeaaaaaaaaagggga" ;  
  138.   Pattern  p3  =  Pattern .compile("a+");  
  139.   Matcher  m3  =  p3 .matcher(str3);  
  140.    boolean  bo4  =  m3 .matches();  
  141.   System.out.println("matches方法:  "+bo4);  
  142.     /*  
  143.    * lookingAt方法,从开头第一个字符进行匹配,匹配成功了不再继续匹配,  
  144.    * 从第一个字符开始,匹配失败了,也不继续匹配.不需要匹配整个序列  
  145.    */  
  146.   boolean  bo5  =  m3 .lookingAt();  
  147.   if(bo5){  
  148.    //group方法(不带参数)返回的就是匹配的子字符串.  
  149.    System.out.println("lookingAt方法:  "+m3.group());  
  150.   }  
  151.     //find方法:找到一个匹配的子串,还会继续找下一个子串.  
  152.   while(m3.find()){  
  153.    System.out.println("find方法:  "+m3.group());  
  154.   }  
  155.     /*  
  156.    * 带参数的group方法与不带参数的group方法区别  
  157.    * 不带参数的group方法:find方法与lookingAt方法匹配出来的子序列(上面有演示)  
  158.    * 带参数的group方法: 返回在以前匹配操作期间由给定组捕获的输入子序列。  
  159.    */  
  160.   String  str6  =  "aaabbbccc" ;  
  161.   Pattern  p5  =  Pattern .compile("(a+)(b+)(c+)");  
  162.   Matcher  m5  =  p5 .matcher(str6);  
  163.   boolean  boo  =  m5 .matches();  
  164.   if(boo){  
  165.    int  k  =  m5 .groupCount()+1;//加1就是把0下标的整个字符序列加上,它也作为一组放在0下标的位置.  
  166.    if(k > 0){  
  167.     for(int  i = 0 ;i     System.out.println(m5.group(i));  
  168.     }  
  169.    }  
  170.   }  
  171.  }  

结果代码

 
  1. true     
  2. true     
  3. true     
  4. a*b     
  5. rrrrr > > > >      
  6. cccc > > > >      
  7. matches方法:  false     
  8. lookingAt方法:  aa     
  9. find方法:  aaaaa     
  10. find方法:  aaaaaaaaa     
  11. find方法:  a     
  12. aaabbbccc     
  13. aaa     
  14. bbb     
  15. ccc  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值