gpt4 book ai didi

java - 从正则表达式匹配器检索捕获组

转载 作者:行者123 更新时间:2023-12-01 18:56:37 25 4
gpt4 key购买 nike

如何解析以下字符串中的两个数字:

String fName = "Run_1_vs_2_pw_optimal_mapping.txt";

我尝试了这样的方法,但不起作用:

    Pattern filePatt = Pattern.compile("Run_(\\d+)_vs_(\\d+)_", Pattern.CASE_INSENSITIVE);

Matcher scanner = this.filePatt.matcher(fName);
while (scanner.find()) {
int groupSize = scanner.groupCount();
if (groupSize == 2) {
firstRun = Integer.parseInt(scanner.group(0));
secondRun = Integer.parseInt(scanner.group(1));
}
break;
}

但是,这不起作用,因为scanner.group(0) 返回Run_1_vs_2。但为什么呢?

最佳答案

参见the documentation .

Capturing groups are indexed from left to right, starting at one. Group zero denotes the entire pattern, so the expression m.group(0) is equivalent to m.group().

使用group(1)group(2)

关于java - 从正则表达式匹配器检索捕获组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13749152/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com