java中使用正则表达式中的组

组是括号划分的正则表达式,可以根据组的编号来引用某个组。组号为0表示整个表达式,组号1表示从左到右被第一个括号扩起的组,以此类推。
例如:
A(B(CD))E中有三个组:组0是ABCDE,组1是BCD,组2是CD。
Matcher对象提供了一系列方法,用以获取与组相关的信息:

方法作用
public int groupCount()返回该匹配器的模式中的分组数目,第0组不包括在内
public String group()返回前一次匹配操作的第0组(整个匹配)
public String group(int i)返回在前一次匹配操作期间的指定的组号
public int start(int group)返回在前一次匹配操作中寻找到的组的起始索引
public int end(int group)返回在前一次匹配操作中寻找到的组的最后一个字符索引加一的值

例子:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GroupsDemo {
    static public final String POEM="Twas brilling, and the slithy toves\n"
    		+ "Did gyre and gimble in the wabe.\n"
    		+ "All mimsy were the borogoves,\n"
    		+ "And the mome raths outgrabe.\n";
    
	public static void main(String[] args) {
		/*
		 * Patten.MULTILINE为模式标记,表示多行模式,在多行模式下,表达式^和$分别匹配一行的开始和结束,也可以匹配输入字符串的开始和结束
		 * \S+表示一次以上的非空格字符,s+表示一次以上的空格字符,目的匹配每行的最后3个字符。
		 */
		Matcher m=Pattern.compile("(\\S+)\\s+((\\S+)\\s+(\\S+))$",Pattern.MULTILINE).matcher(POEM);		
		while (m.find()) {
             for (int i = 0; i <=m.groupCount(); i++) {
            	 System.out.print("第"+i+"组是:"+"["+m.group(i)+"]   ");		
			}
        	 System.out.println();
		}
         
	}

}

运行结果:

0组是:[the slithy toves]1组是:[the]2组是:[slithy toves]3组是:[slithy]4组是:[toves]0组是:[in the wabe.]1组是:[in]2组是:[the wabe.]3组是:[the]4组是:[wabe.]0组是:[were the borogoves,]1组是:[were]2组是:[the borogoves,]3组是:[the]4组是:[borogoves,]0组是:[mome raths outgrabe.]1组是:[mome]2组是:[raths outgrabe.]3组是:[raths]4组是:[outgrabe.]   

start()和end()的使用:
在匹配操作成功之后,start()返回先前匹配的起始位置的索引,而end()返回所匹配的最后字符的索引加一的值。如果匹配操作失败后(或先于一个正在进行的匹配操作去操作)调用start()或end()将会产生IllegalStateException.
下面是使用例子:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GroupsDemo {
    static public final String POEM="Twas brilling, and the slithy toves\n"
    		+ "Did gyre and gimble in the wabe.\n"
    		+ "All mimsy were the borogoves,\n";
    
	public static void main(String[] args) {
		/*
		 * Patten.MULTILINE为模式标记,表示多行模式,在多行模式下,表达式^和$分别匹配一行的开始和结束,也可以匹配输入字符串的开始和结束
		 * \S+表示一次以上的非空格字符,s+表示一次以上的空格字符,目的匹配每行的最后3个字符。
		 */
		Matcher m=Pattern.compile("(\\S+)\\s+((\\S+)\\s+(\\S+))$",Pattern.MULTILINE).matcher(POEM);		
		while (m.find()) {
       	    System.out.print("起始索引为:"+m.start());
       	    System.out.println("结束索引为:"+m.end());
       	    
             for (int i = 0; i <=m.groupCount(); i++) {
            	 System.out.print("第"+i+"组是:"+"["+m.group(i)+"]   ");	
            	    System.out.print("该组的起始索引为:"+m.start(i));
            	    System.out.println("该组的结束索引为:"+m.end(i));
			}
        	 System.out.println();
		}
         
	}

}

运行结果:

起始索引为:19结束索引为:350组是:[the slithy toves]   该组的起始索引为:19该组的结束索引为:351组是:[the]   该组的起始索引为:19该组的结束索引为:222组是:[slithy toves]   该组的起始索引为:23该组的结束索引为:353组是:[slithy]   该组的起始索引为:23该组的结束索引为:294组是:[toves]   该组的起始索引为:30该组的结束索引为:35

起始索引为:56结束索引为:680组是:[in the wabe.]   该组的起始索引为:56该组的结束索引为:681组是:[in]   该组的起始索引为:56该组的结束索引为:582组是:[the wabe.]   该组的起始索引为:59该组的结束索引为:683组是:[the]   该组的起始索引为:59该组的结束索引为:624组是:[wabe.]   该组的起始索引为:63该组的结束索引为:68

起始索引为:79结束索引为:980组是:[were the borogoves,]   该组的起始索引为:79该组的结束索引为:981组是:[were]   该组的起始索引为:79该组的结束索引为:832组是:[the borogoves,]   该组的起始索引为:84该组的结束索引为:983组是:[the]   该组的起始索引为:84该组的结束索引为:874组是:[borogoves,]   该组的起始索引为:88该组的结束索引为:98

参考书籍:《Thinking in Java》

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值