【发布时间】:2016-02-24 23:34:24
【问题描述】:
如果我有例如 1776 年 7 月 4 日是美国的独立日 今天的日期是 2016 年 2 月 25 日,我怎么可能从句子中得到“1776 年 7 月 4 日”和“2016 年 2 月 25 日”并将它们更改为 yyyy_date_month 的格式?
public final static String regex = StringUtils.join(new String[]{
String.format("(%s)", StringUtils.join(
//date
"((3[01]|2\\d|1\\d|0?\\d)" +
//ordinals
"(st|nd|rd|th)?(\\sof)?" +
// month,
"(\\s?(jan(uary)?|feb(ruary)?|mar(ch)?|apr(il)?|may|jun(e)|jul(y)|aug(ust)?|sep(tember)?|oct(ober)?|nov(ember)?|dec(ember)?))"+
//year
"?(\\d{4})?)"
)),
String.format("(%s)", StringUtils.join(//mm/dd/yyyy format
//month/
"^(0[1-9]|1[0-2])" +
// - | /
"(\\/|-)?" +
//date/
"(\\s0[1-9]|\\s1\\d|\\s2\\d|\\s3[01])" +
// - | /
"(\\/|-)?" +
//year
"(\\s\\d{4})"
}, "|");
public Dates() {
Pattern[] patterns = new Pattern[regex.length()];
for (int i = 0; i < regex.length(); i++) {
patterns[i] = Pattern.compile(regex);
}
this.patterns = patterns;
}
【问题讨论】: