此处使用的正确模式是:(\\d+)\\s+cm\\b对于一个班轮,我们可以尝试使用String#replaceAll:String input = "Rectangle Width is 100 cm, Some text";String output = input.replaceAll(".*?(\\d+)\\s+cm\\b.*", "$1");System.out.println(output);或者,要查找给定文本中的所有匹配项,我们可以尝试使用正式的模式匹配器:String input = "Rectangle Width is 100 cm, Some text";String pattern = "(\\d+)\\s+cm\\b";Pattern r = Pattern.compile(pattern);Matcher m = r.matcher(input);while (m.find()) { System.out.println("Found measurement: " + m.group(1));}