月份模式日期的正则表达式

时间:2013-11-18 01:50:52

标签: java regex date

我的任务是从resume(txt文件)中查找日期并计算它们之间的持续时间 我已经创建了持续时间(日期)模式的正则表达式 2012年1月至今或2012年1月 - 2013年1月或1/2013 - 2014年2月或2012年1月1日 - 2014年2月 这是我的正则表达式

private String monthPattern = "((0[1-9]|1[012]|[1-9])|(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sept|Sep|Oct|Nov|Dec)[a-z]*)";  // 3 groups

private String monthAndYearSeperator="\\s*(\\s*|,|;|~|--|-|.|\\/)\\s*";      // 1 group
private String twoOrFourDigitYearPattern="(19[0-9]{2}|[2-9][0-9]{3}|[0-9]{2})\\s*";         // 1 group  
private String presentPattern = "(Current|Present|Now|Currently|Presently|Till Date|Todate|Today)";
private String twoDatesSeperator = "\\s*(\\s*|-|~|--|,|to|til|till|until)\\s*";    // 1 group
private String twoOrFourDigitOrPresentYearPattern = presentPattern + "|" + twoOrFourDigitYearPattern;  // 2 groups

private String secondIdenticalMonthPattern="(([1-9]|0[1-9]|1[012])|(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sept|Sep|Oct|Nov|Dec|January|February|March|April|May|June|July|August|September|October|November|December))";  // 3 groups

使用上述模式我创建了一个持续时间模式

private String dateToDateCompletePatternOne=
    monthPattern +  monthAndYearSeperator +
    twoOrFourDigitYearPattern +
    twoDatesSeperator + "((" + secondIdenticalMonthPattern +      
    monthAndYearSeperator +
    twoOrFourDigitYearPattern +")|" +
    presentPattern +")" 
;

现在我的问题是当我的简历文件包含2013年1月的模式时,那么我的正则表达式也匹配这种日期但实际上它不匹配

请帮助我,因为我两周以来一直在努力

请找到以下代码

import java.io.IOException;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.concurrent.CountDownLatch;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.w3c.dom.css.Counter;

public class DatePattens {
    //private ArrayList<MatchedDateObject> arryLstOfDates = new ArrayList<MatchedDateObject>();
    private ArrayList<String> matchedString = new ArrayList<String>();
    private HashMap<String,Integer> map ;


    private String monthPattern = "((0[1-9]|1[012]|[1-9])|(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sept|Sep|Oct|Nov|Dec)[a-z]*)";  // 3 groups
    private String monthAndYearSeperator="\\s*(\\s*|,|;|~|--|-|.|\\/)\\s*";      // 1 group
    private String twoOrFourDigitYearPattern="(19[0-9]{2}|[2-9][0-9]{3}|[0-9]{2})\\s*";         // 1 group  
    private String presentPattern = "(Current|Present|Now|Currently|Presently|Till Date|Todate|Today)";
    private String twoDatesSeperator = "\\s*(\\s*|-|~|--|,|to|til|till|until)\\s*";    // 1 group
    private String twoOrFourDigitOrPresentYearPattern = presentPattern + "|" + twoOrFourDigitYearPattern;  // 2 groups
    private String secondIdenticalMonthPattern="(([1-9]|0[1-9]|1[012])|(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sept|Sep|Oct|Nov|Dec|January|February|March|April|May|June|July|August|September|October|November|December))";  // 3 groups
    private String dateToDateCompletePatternOne=
    monthPattern + monthAndYearSeperator + twoOrFourDigitYearPattern + twoDatesSeperator +  
    "((" + secondIdenticalMonthPattern +
    monthAndYearSeperator +
    twoOrFourDigitYearPattern +")|" +
    presentPattern +")" 
;               
    private Pattern patternAry = null;
    private Matcher matcher = null;
    public DatePattens() {
        map = new HashMap<String,Integer>();
        patternAry = Pattern.compile(dateToDateCompletePatternOne, Pattern.CASE_INSENSITIVE);
        matcher = patternAry.matcher("");   
    }
    //
    // extract the two dates to look for duration afterwards
    // 1. check if the a year pattern exists
    //    1.1 if not skip to else at the end and return false
    // 2. if yes get the rest of the line past year 1
    // 3. check for year 2 or CURRENT/Present/...

    public boolean matchTwoYearPattern(String inputLine){
        String fname="matchTwoYearPattern";
        Pattern firstYearPattern = Pattern
                .compile(twoOrFourDigitYearPattern,Pattern.CASE_INSENSITIVE);
        Matcher matcher1 = firstYearPattern.matcher("");


        Pattern secondPattern = Pattern.compile(twoOrFourDigitOrPresentYearPattern,
                Pattern.CASE_INSENSITIVE);
        Matcher matcher2 = secondPattern.matcher("");
        //long startTime = System.currentTimeMillis();

        matcher1.reset(inputLine);
        if (matcher1.find()) {  // 1
            String remaingString = inputLine.substring(matcher1.end(),
                    inputLine.length());   // 2
            matcher2.reset(remaingString);
            if (matcher2.find()) {  // 3
                return true;
            }

        }       
        return false;   // 1.1 and end 

    }
    public String matchAllDatePatterns(String line, int lineNum){
        String fname = "matchAllPatterns:: ";
         if (matchTwoYearPattern(line) == false) {  // check if two years (or year and CURRENT/today...) present, if not return false
             return("false:" + line);
         }
         else {
         }
        String matched = "";
        int i = 0;
            matcher.reset(line);
            if (matcher.find()) {
             System.out.println(fname + "line: " +line);
                System.out.println("group count "+matcher.groupCount());                
                System.out.println("group1 " +matcher.group(1));
                System.out.println("group2 " +matcher.group(2));
                System.out.println("group3 " +matcher.group(3));
                System.out.println("group4 " +matcher.group(4));
                System.out.println("group5 " +matcher.group(5));
                System.out.println("group6 " +matcher.group(6));
                System.out.println("group7 " +matcher.group(7));
                System.out.println("group8 " +matcher.group(8));
                System.out.println("group9 " +matcher.group(9));
                System.out.println("group10 " +matcher.group(10));
                System.out.println("group11 " +matcher.group(11));
                System.out.println("group12 " +matcher.group(12));
                System.out.println("group13 " +matcher.group(13));
                System.out.println("group14 " + matcher.group(14));        
        }

            return matched;

    }
    public static void main(String args[]){
        DatePattens dp= new DatePattens();
        String fileName = "Resume.txt";

        try {
            ReadFile file = new ReadFile(fileName);
            String[] aryLines = file.openFile();
            int i=0;
             long startTime =System.currentTimeMillis();

            for(int count=0;count<1;count++){
                for (String input : aryLines) {
                    String output = dp.matchAllDatePatterns(input, i);
                    i++;
                }
            }
            long endTime =System.currentTimeMillis();
            System.out.println("Time required for this operation :" + ((endTime-startTime)*0.001));

        } catch (IOException e) {
            System.out.println(e);
        }

    }

}

0 个答案:

没有答案