java 正则表达式 matcher.group_Pattern Matcher group 简单例子正则表达式

多次使用了Matcher匹配后的group提取需要的内容。记录一下非常简单的demo。

至于正则表达式请参考其他资料,本代码片段使用了\d匹配数字 (\d+)表示匹配一个或多个数字。

https://docs.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html#groupCount()

public int groupCount()

Returns the number of capturing groups in this matcher’s pattern.

Group zero denotes the entire pattern by convention. It is not included in this count.(group 0表示这个匹配pattern的整个字符串。不包括在内)

Any non-negative integer smaller than or equal to the value returned by this method is guaranteed to be a valid group index for this matcher.

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class AbsMain {

/** *@param args */

final static public String CREATED_DESC = "Created for Step '%s' of project '%s'";

final static public String MATCHER_PATTERN = "Created for Step '(\\d+)' of project '(\\d+)'";

public static void main(String[] args) throws IOException {

String str0 = String.format(CREATED_DESC,3,5);

String str1 = String.format(CREATED_DESC,5,78);

String str2 = String.format(CREATED_DESC,6,718);

String str3 = String.format(CREATED_DESC,31,6);

String str4 = String.format(CREATED_DESC,69);

String str5 = String.format(CREATED_DESC,391,62);

String str6 = String.format(CREATED_DESC,367,3);

String str7 = String.format(CREATED_DESC,361,50);

String str8 = String.format(CREATED_DESC,320,503);

String str9 = String.format(CREATED_DESC,36716,37890);

Pattern pattern = Pattern.compile(MATCHER_PATTERN);

Matcher matcher = null;

String[] strArray = {str0,str1,str2,str3,str4,str5,str6,str7,str8,str9};

int matchCount = 0;

int totalCount = strArray.length;

for (String str : strArray) {

System.out.println("Current str:" + str);

matcher = pattern.matcher(str);

if (matcher.matches()) {

matchCount++;

int groupCount = matcher.groupCount();

for (int i = 0 ;i < groupCount + 1; i++) {

System.out.println("TotalGroup:" + groupCount +",Group:" + i+ ",Value:"+ matcher.group(i));

}

}

else {

System.out.println("can't match with pattern.");

}

System.out.println("----------------------------");

System.out.println("");

}

System.out.println("Total StringArray count:" + totalCount + ",matched pattern count:" + matchCount);

}//main

}//class

运行结果:

Current str:Created for Step ‘3’ of project ‘5’

TotalGroup:2,Group:0,Value:Created for Step ‘3’ of project ‘5’

TotalGroup:2,Group:1,Value:3

TotalGroup:2,Group:2,Value:5

Current str:Created for Step ‘5’ of project ‘78’

TotalGroup:2,Value:Created for Step ‘5’ of project ‘78’

TotalGroup:2,Value:5

TotalGroup:2,Value:78

Current str:Created for Step ‘6’ of project ‘718’

TotalGroup:2,Value:Created for Step ‘6’ of project ‘718’

TotalGroup:2,Value:6

TotalGroup:2,Value:718

Current str:Created for Step ‘31’ of project ‘6’

TotalGroup:2,Value:Created for Step ‘31’ of project ‘6’

TotalGroup:2,Value:31

TotalGroup:2,Value:6

Current str:Created for Step ‘31’ of project ‘69’

TotalGroup:2,Value:Created for Step ‘31’ of project ‘69’

TotalGroup:2,Value:31

TotalGroup:2,Value:69

。。。 Total StringArray count:10 matched pattern count:10

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值