我在java中使用regex制作日期提取器。问题是日期是20-05-2014,我的程序正在提取0-5-14。简而言之,我如何获得我正在检查日期的第二个字符的字符?
int count = 0;
String data = "HellowRoldsThisis20-05-2014. farhan_rock@gmail.comHellowRoldsThisis.farhan@gmail.com";
String regexOfDate = "((?<=[0])[1-9]{2})|((?<=[12])[0-9])|((?<=[3])[01])\\.\\-\\_((?<=[0])[1-9])|((?<=[1])[0-2])\\.\\-\\_((?<=[2])[0-9]{4})"; \\THE PROBLEM
String[] extractedDate = new String[1000];
Pattern patternDate = Pattern.compile(regexOfDate);
Matcher matcherDate = patternDate.matcher(data);
while(matcherDate.find()){
System.out.println("Date "+count+"Start: "+matcherDate.start());
System.out.println("Date "+count+"End : "+matcherDate.end());
extractedDate[count] = data.substring(matcherDate.start(), matcherDate.end());
System.out.println("Date Extracted: "+extractedDate[count]);
}
答案 0 :(得分:2)
您可以尝试使用正则表达式:
// (0[1-9]|[12][0-9]|[3][01])[._-](0[1-9]|1[0-2])[._-](2[0-9]{3})
"(0[1-9]|[12][0-9]|[3][01])[._-](0[1-9]|1[0-2])[._-](2[0-9]{3})"
答案 1 :(得分:0)
匹配有效日期的单个正则表达式为awful。
我会这样做:
String regexOfDate = "(?<!\\d)\\d{2}[-_.]\\d{2}[-_.]\\d{4}(?!\\d)";
提取潜在日期,然后测试它是否有效。