java groupcount()等于0_Java Matcher groupCount()用法及代码示例

Matcher Class的groupCount()方法用于获取此Matcher模式中的捕获组数。

用法:

public int groupCount()

参数:此方法不带任何参数。

返回值:此方法返回此匹配器模式中的捕获组数。

下面的示例说明Matcher.groupCount()方法:

示例1:

// Java code to illustrate groupCount() method

import java.util.regex.*;

public class GFG {

public static void main(String[] args)

{

// Get the regex to be checked

String regex = "Geeks";

// Create a pattern from regex

Pattern pattern

= Pattern.compile(regex);

// Get the String to be matched

String stringToBeMatched

= "GeeksForGeeks";

// Create a matcher for the input String

Matcher matcher

= pattern

.matcher(stringToBeMatched);

// Get the number of capturing groups

// using groupCount() method

System.out.println(matcher.groupCount());

}

}

输出:

0

示例2:

// Java code to illustrate groupCount() method

import java.util.regex.*;

public class GFG {

public static void main(String[] args)

{

// Get the regex to be checked

String regex = "GFG";

// Create a pattern from regex

Pattern pattern

= Pattern.compile(regex);

// Get the String to be matched

String stringToBeMatched

= "GFGFGFGFGFGFGFGFGFG";

// Create a matcher for the input String

Matcher matcher

= pattern

.matcher(stringToBeMatched);

// Get the number of capturing groups

// using groupCount() method

System.out.println(matcher.groupCount());

}

}

输出:

0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中的group是正则表达式中的一个概念,表示匹配到的子串。在Matcher类中,group有多个重载方法,可以根据组号或组名获取匹配到的子串,也可以获取子串在原字符串中的起始和结束位置。具体方法如下: - int groupCount():返回匹配到的组数,不包括group(0)。 - String group(int group):返回指定组号的匹配子串。 - int start(int group):返回指定组号匹配子串在原字符串中的起始位置。 - int end(int group):返回指定组号匹配子串在原字符串中的结束位置。 - String group(String name):返回指定组名的匹配子串。 - int start(String name):返回指定组名匹配子串在原字符串中的起始位置。 - int end(String name):返回指定组名匹配子串在原字符串中的结束位置。 例如,对于字符串"John writes Java code",使用正则表达式"(\w+) writes (\w+) code"进行匹配,可以得到两个组,分别是"John"和"Java"。代码如下: ```java String str = "John writes Java code"; Pattern pattern = Pattern.compile("(\\w+) writes (\\w+) code"); Matcher matcher = pattern.matcher(str); if (matcher.find()) { System.out.println("found group: group(0) is '" + matcher.group(0) + "'"); System.out.println("found group: group(1) is '" + matcher.group(1) + "', group(2) is '" + matcher.group(2) + "'"); System.out.println("groupCount is ->" + matcher.groupCount()); System.out.println("start(1) is ->" + matcher.start(1) + ", end(1) is ->" + matcher.end(1)); System.out.println("start(2) is ->" + matcher.start(2) + ", end(2) is ->" + matcher.end(2)); } ``` 输出结果为: ``` found group: group(0) is 'John writes Java code' found group: group(1) is 'John', group(2) is 'Java' groupCount is ->2 start(1) is ->0, end(1) is ->4 start(2) is ->12, end(2) is ->16 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值