我正在尝试使用正则表达式从字符串中提取 2 个日期 - 由于某种原因 - 正则表达式不提取日期 - 这是我的代码:
private String[] getDate(String desc) {
int count=0;
String[] allMatches = new String[2];
Matcher m = Pattern.compile("(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\\d\\d(?:,)").matcher(desc);
while (m.find()) {
allMatches[count] = m.group();
}
return allMatches;
}
我的 string-desc 是:“coming from the 11/25/2009 to the 11/30/2009”
然后我得到一个空数组...
最佳答案
您的正则表达式首先匹配日,然后匹配月 (DD/MM/YYYY),而您的输入从月开始,然后是日 (MM/DD/YYYY)。
此外,您的日期后面必须跟一个逗号才能匹配((?:,)
部分)。
这个应该适合您的需求:
(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d
图表 Debuggex .
关于Java - 使用正则表达式从字符串中提取日期 - 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18591242/