您可以尝试使用模式匹配import java.util.regex.Pattern;import java.util.regex.Matcher;// ... Other featuresString fileName = "..."; // Filename with number extensionPattern pattern = Pattern.compile("^.*(\\d+).*$"); // Pattern to extract number// Then try matchingMatcher matcher = pattern.matcher(fileName);String numberExt = "";if(matcher.matches()) { numberExt = matcher.group(1);} else { // The filename has no numeric value in it.}// Use your numberExt here.
您可以使用正则表达式将每个数字部分与字母数字部分分开:public static void main(String args[]) { String str = "ABC.12.txt.gz"; String[] parts = str.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)"); // view the resulting parts for (String s : parts) { System.out.println(s); } // do what you want with those values...}这将输出ABC.12.txt.gz然后拿走你需要的零件,用它们做你必须做的事。