正则表达式无法从较大的字符串中提取日期

时间:2017-02-22 09:44:30

标签: java regex date parsing

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中提到的日期,在字符串简历中存储简历。

1 个答案:

答案 0 :(得分: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"

希望这有帮助!