您需要在正则表达式中添加捕获组.这是通过将您想要捕获的内容放在括号中来完成的:
Pattern pattern = Pattern.compile("(.*):.*"); // <-- parentheses here String name = "abc:def"; Matcher matcher = pattern.matcher(name); if (matcher.find()) { String group = matcher.group(1); System.out.println(group); // prints "abc" }