用Patternmatches判断6-一月-23这样类型字符串日期格式要求其他月份日期年也匹配
可以使用正则表达式来判断是否符合指定的日期格式。
以下是一个示例代码,使用Pattern.matches方法来判断字符串是否符合格式"数字-月份-数字":
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String input = "6-一月-23";
String pattern = "\\d+-\\p{InHiragana}+-\\d+";
boolean isMatch = Pattern.matches(pattern, input);
System.out.println(isMatch);
}
}
在上面的代码中,使用了正则表达式\d+-\p{InHiragana}+-\d+,其中\d表示数字,\p{InHiragana}表示任意一个平假名字符(日本字符)。这个正则表达式可以匹配类似"6-一月-23"的日期格式。
运行上面的代码,输出结果为true,表示字符串"6-一月-23"符合要求的日期格式
原文地址: https://www.cveoy.top/t/topic/iT0x 著作权归作者所有。请勿转载和采集!