如果您需要匹配這些日期與應用的任何格式,你需要考慮更多的空白,並在您的正則表達式的任何其他文本:
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"
希望這有助於!
你可以添加示例輸入嗎? – vagelis
String word =「要記住的一件重要的事情;關於這個Java匹配方法,你的正則表達式必須匹配28-3-2014到28-3-2017整行,特別是正則表達式) w;當你處理更大的輸入文本時,不適用於匹配方法「; 它提取日期從上面的字符串,但它不是工作簡歷其中包括在同一格式的日期。 如果日期如2年或2年2個月它工作正常 – priya
當正則表達式不匹配時,恢復字符串有多大? – freedev