private static Pattern timePattern =
Pattern.compile("(?<=,)[一|二|三|四|五|六|七|八|九|十]+[个月|天|年]");
private static Pattern codePattern =
Pattern.compile("\\d+");
public static void main(String[] args) {
String msg="6972549660134,一年";
Matcher productCodeMatcher = codePattern.matcher(msg);
if (productCodeMatcher.find()) {
System.out.println("码: " + productCodeMatcher.group());
}
Matcher timeMatcher = timePattern.matcher(msg);
if (timeMatcher.find()) {
System.out.println("时间: " + timeMatcher.group());
}
}
JAVA正则 提取字符串里的数字和中文
于 2023-11-16 10:35:17 首次发布