我有方法,它接受日期作为一个字符串,我的目标是找出月份类型为数字或字符串像01.02 ....或1月,二月,所以我需要比较这个月的价值正则表达式,但不幸的是,如果月份字段包含数值然后match.find()不起作用,但为什么?提前致谢为什么这个正则表达式不起作用?
public static String detectMonthType(String date) {
String[] parts = {};
if(date.contains("/")) {
parts = date.split("/");
} else {
parts = date.split("-");
}
String dateType = "";
String month = parts[1];
String strPattern = "^(0|[1-9][0-9]*)$";
Pattern pattern = Pattern.compile(strPattern);
Matcher match = pattern.matcher(month);
if(match.find()) {
return dateType = "numericMonth";
} else {
return dateType = "strMonth";
}
}
什么是你的输入? – shmosel
我的输入是07/11/16 –
您的预期输出是什么? – shmosel