从较大的字符串提取日期时,REGEX失败了


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();
}

它适用于较小的字符串,但在这里我需要确定简历中提到的日期。我存储在弦简历中。

如果您需要将这些日期与所应用的任何格式匹配,则需要考虑到更多的whitespace和Regex中的任何其他文本:

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"

希望这会有所帮助!

最新更新