Java Matcher 可以通过 group() 方法获取匹配到的子字符串。
例如:
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] args) { String text = "Hello, world! This is a test string."; Pattern pattern = Pattern.compile("\\b\\w+\\b"); // 匹配单词 Matcher matcher = pattern.matcher(text); while (matcher.find()) { String match = matcher.group(); // 获取匹配到的子字符串 System.out.println(match); } } }上面的代码中,我们使用 Matcher 进行匹配,并通过 group() 获取匹配到的子字符串,然后将其输出到控制台。
提供PHP及ThinkPHP框架的定制开发、代码优化,PHP修改、ThinkPHP修改。