2017-02-22 51 views
0
Matcher m = Pattern.compile("\\d{1,2} years \\d months|\\d{1,2} years|" 
       + "\\d{1,2}-\\d{1,2}-\\d{2,4}\\s+to\\s+\\d{1,2}-\\d{1,2}-\\d{2,4}").matcher(resume); 

while (m.find()){ 
    experience = m.group(); 
} 

它適用於較小的字符串,但在這裏我需要確定resume.i中提到的日期存儲在字符串簡歷中的簡歷。正則表達式從大字符串中提取日期失敗

+0

你可以添加示例輸入嗎? – vagelis

+0

String word =「要記住的一件重要的事情;關於這個Java匹配方法,你的正則表達式必須匹配28-3-2014到28-3-2017整行,特別是正則表達式) w;當你處理更大的輸入文本時,不適用於匹配方法「; 它提取日期從上面的字符串,但它不是工作簡歷其中包括在同一格式的日期。 如果日期如2年或2年2個月它工作正常 – priya

+0

當正則表達式不匹配時,恢復字符串有多大? – freedev

回答

0

如果您需要匹配這些日期與應用的任何格式,你需要考慮更多的空白,並在您的正則表達式的任何其他文本:

Matcher m = Pattern.compile(
    "^.*?" + // Start of line, then anything, non-greedy. 
    "(?:" + // Non-capturing group 
    "\\d{1,2}\\s*years(?:[,\\s]*\\d{1,2}\\s*months)?|" + // Years with optional months 
    "\\d{1,2}\\s*[\\-/]{1}\\d{1,2}\\s*[\\-/]{1}\\d{2,4}\\s*to\\s*" + // From to To, 1/2 
    "\\d{1,2}\\s*[\\-/]{1}\\d{1,2}\\s*[\\-/]{1}\\d{2,4}" + // From to To 2/2 
    ")" + // Non-capturing group closes 
    ".*$" // Anything else up to the end of the line 
).matcher(""); 

如果您需要正則表達式匹配行,你必須用線喂Matcher

BufferedReader reader = new BufferedReader(new StringReader(resume)); 
String line; 
while ((line = reader.readLine()) != null) { 
    if (matcher.reset(line).matches()) { 
    experience = matcher.group(); 
    } 
} 

示例匹配:

" 5 years" 
"12 years, 10 months." 
" 10/12/2010 to 3/2/12: Blah" 

希望這有助於!

+0

我試過ur方法,但它的返回null.If我嘗試它在一個單獨的java類中執行,但不是在我的class.why? @vagelis – priya

+0

我認爲這很簡單:1)確保'resume'是包含**換行**的文本。 2)用'Matcher m = Pattern.compile(「」).matcher(「」)'(我的答案在那裏是錯誤的,我會糾正它)準備'Matcher'。 3)使用「BufferedReader」 - 「StringReader」組合逐行提供簡歷。 – vagelis

+0

我可以使用nlp嗎?如果是這樣如何? – priya