Java-正则表达式从字符串中提取日期 [英] Java - Regex extract date from string
问题描述
我需要从该字符串中提取日期:
I need to extract date from this string:
BB通知:购买您的壁虱,最终卡号xxxx,$ 00,00,于04/10,发布于11:28。如果您不认识,请致电40032 2412。
BB inform: buy your tickect, final card number xxxx, $ 00,00, on 04/10, at 11:28. If you don't recognize call 40032 2412.
也请完整日期2015年10月10日
Also The full date 04/10/2015
日期模式为dd / MM或dd / MM / yyyy
The date pattern is dd/MM or dd/MM/yyyy
代码:
String mydata = BB通知:在04/10的11:28购买您的壁虱,最终卡号xxxx,$ 00,00。如果您不认识,请致电40032 2412。;
String mydata = "BB inform: buy your tickect, final card number xxxx, $ 00,00, on 04/10, at 11:28. If you don't recognize call 40032 2412.";
Pattern p = Pattern.compile("(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\\d\\d");
Matcher m = p.matcher(mydata);
结果:
m.matches()== false
Result: m.matches() == false
推荐答案
您可以尝试使用此正则表达式:
Matcher m = Pattern.compile("(\\d{1,2}/\\d{1,2}/\\d{4}|\\d{1,2}/\\d{1,2})", Pattern.CASE_INSENSITIVE).matcher(string);
while (m.find()) {
System.out.println(m.group(1));
}
它正在寻找图案DD / MM或DD / MM / YYYY。
Its looking for a pattern DD/MM or then looking for a DD/MM/YYYY.
选中此链接
这篇关于Java-正则表达式从字符串中提取日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!