Matcher start() end() group() groupCount()

public int start()         这个方法返回了,Matcher所匹配的字符串在整个字符串的的开始下标: 


下面我们看看一个小例子 
public class MatcherStartExample{
  public static void main(String args[]){
      test();
  }
  public static void test(){
     //create a Matcher and use the Matcher.start() method
     String candidateString = "My name is Bond. James Bond.";
     String matchHelper[] =
      {"          ^","                      ^"};
     Pattern p = Pattern.compile("Bond");
     Matcher matcher = p.matcher(candidateString);
     //Find the starting point of the first 'Bond'
      matcher.find();
      int startIndex = matcher.start();
      System.out.println(candidateString);
      System.out.println(matchHelper[0] + startIndex);
     //Find the starting point of the second 'Bond'
      matcher.find();
      int nextIndex = matcher.start();
      System.out.println(candidateString);
      System.out.println(matchHelper[1] + nextIndex);
}
输出结果: 
My name is Bond. James Bond.
           ^11 
My name is Bond. James Bond.
                       ^23 

我自己改了这个例子
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestPatternAndMatcher3 {

    public static void test(){
        String candidateString = "My name is Bond. James Bond.";
        String matchHelper[] =  {"          ^", + 
                         "                      ^"};

        Pattern p = Pattern.compile("Bond");
        Matcher matcher = p.matcher(candidateString);

        matcher.find();
        int startIndex = matcher.start();
        System.out.println(candidateString);
        System.out.println(matchHelper[0] + startIndex);

        char[] al = candidateString.toCharArray();
        System.out.println("al[startIndex] : " + al[startIndex]);

        matcher.find();
        int nextIndex = matcher.start();
        System.out.println(candidateString);
        System.out.println(matchHelper[1] + nextIndex);

        char[] al2 = candidateString.toCharArray();
        System.out.println("al2[startIndex] : " + al2[startIndex]);
   }

public static void main(String[] args) {

    test();

}


}
</pre><pre class="java" name="code" style="color: rgb(102, 102, 102); background-color: rgb(255, 255, 255);">import java.util.regex.*;
public class MatcherGroupExample{
  public static void main(String args[]){
      test();
  }
  public static void test(){
      //create a Pattern
      Pattern p = Pattern.compile("Bond");
      //create a Matcher and use the Matcher.group() method
      String candidateString = "My name is Bond. James Bond.";
      Matcher matcher = p.matcher(candidateString);
      //extract the group
      matcher.find();
      System.out.println(matcher.group());
  }
}

public int groupCount()

这个方法返回了,正则表达式的匹配的组数。 

妈的这个微博排版这么费事!!!


原博客地址http://www.java3z.com/cwbwebhome/article/article8/81313.html?id=3138



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值