gpt4 book ai didi

java - 对捕获组的主题感到困惑吗?

转载 作者:行者123 更新时间:2023-12-01 14:17:31 24 4
gpt4 key购买 nike

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

public class RegexMatches
{
public static void main( String args[] ){

// String to be scanned to find the pattern.
String line = "This order was places for QT3000! OK?";
String pattern = "(.*)(\\d+)(.*)";

// Create a Pattern object
Pattern r = Pattern.compile(pattern);

// Now create matcher object.
Matcher m = r.matcher(line);
if (m.find( )) {
System.out.println("Found value: " + m.group(0) );
System.out.println("Found value: " + m.group(1) );
System.out.println("Found value: " + m.group(2) );
} else {
System.out.println("NO MATCH");
}
}
}

我想知道 Pattern 和 Matcher 对象是如何工作的?

我举了几个例子,但我想不出来。

最佳答案

您的组从索引 = 1 开始。零是整场比赛的索引。

因此,第一个 (.*) 位于 m.group(1) 中,(\\\d+) 位于 m.group(2) 中,第二个 (.*) 位于 m.group(3)

关于java - 对捕获组的主题感到困惑吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17990329/

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