2014-12-13 51 views
2

我正在java中使用正則表達式提取日期提取器。問題是這個日期是20-05-2014,我的程序正在提取0-5-14。總之,我怎麼能得到我在檢查日期的第二個字符的角色?正則表達式:如何包含「如果」條件的字符

int count = 0; 
    String data = "HellowRoldsThisis20-05-2014. [email protected]@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]); 
    } 

回答

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})" 

Debuggex

+0

如何繪製正則表達式? – Kyslik 2014-12-13 18:58:21

0

單一的正則表達式匹配Ø有效日期是awful

我會怎麼做:

String regexOfDate = "(?<!\\d)\\d{2}[-_.]\\d{2}[-_.]\\d{4}(?!\\d)"; 

提取潛在的日期,然後測試,如果它是有效的。

相關問題