基于正则表达式的简历解析工具

package fun.pkg;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AnalysisResume {
    public static void main(String[] args) {
        System.out.println(t("1965-1979年 地球防卫军副主席,地球防卫军远东战区副总司令、远东战区指挥员"));
        System.out.println(t("1965年5月,王五出生于东华省武阳市的一个普通知识分子家庭,父母毕业于武阳理工大学从事水利、水电的地质勘测工作。"));
        System.out.println(t("1965年7月生于中国长恒省,祖籍东华省,远东外国语大学毕业。"));
        System.out.println(t("1966-1966年 空军第一航空预备学校学员"));
        System.out.println(t("1966-1967年 地球防卫军先锋军第三师三十五团当兵锻炼"));
        System.out.println(t("1966年2月26日,张三出生于富江省安屯市兰溪县(今豫东市)。"));
        System.out.println(t("1966年3月,出生于北川省湖中市怀阳区明阳镇。"));
        System.out.println(t("1966年,“第二次降临”中受冲击;"));
        System.out.println(t("1966年夏在弘阳初中毕业, 恰逢“二降”爆发;"));
        System.out.println(t("1967-1968年 空军第八航空学校学员"));
        System.out.println(t("1967年3月-1971年1月任苏溪省桓阳武东服装厂职工;"));
        System.out.println(t("1967年3月,李四出生于富江省临安市,原籍江北千荣。"));
        System.out.println(t("1967年--1979.12  ? 远东战区黑木建筑队木匠"));
        System.out.println(t("2006.12--2012.03,湖东省南岛战区指挥员、南岛市常务副市长(2006.12.29任命,2008.01.10当选)、市政府党组副书记;"));
        System.out.println(t("1999年08月—2003年07月,苏溪省建筑工程总公司(建设集团公司)副总经理(其间:2001年09月—2002年12月苏溪省军校军事指挥专业在职研究生学习);"));
    }

    public static Result t(String s) {
        // 两个时间的
        Pattern pattern2year = Pattern.compile("((19|20)\\d\\d)(.+?)((19|20)\\d\\d)(.+)");
        Matcher matcher2year = pattern2year.matcher(s);

        // 只有一个时间的
        Pattern pattern1year = Pattern.compile("((19|20)\\d\\d)(.+)");
        Matcher matcher1year = pattern1year.matcher(s);

        // 月份匹配
        Pattern patternMonth = Pattern.compile("(\\d+)(.+)");

        Result result = new Result();

        if (matcher2year.find()) {
            // 匹配出两个年份
            String year1 = matcher2year.group(1);
            String content1 = matcher2year.group(3);
            String year2 = matcher2year.group(4);
            String content2 = matcher2year.group(6);

            // 根据年份分段结果匹配月份
            Matcher matcherMonth1 = patternMonth.matcher(content1);
            Matcher matcherMonth2 = patternMonth.matcher(content2);
            if (matcherMonth1.find()) {
                result.setMonth1(matcherMonth1.group(1));
            }
            if (matcherMonth2.find()) {
                result.setMonth2(matcherMonth2.group(1));
                result.setContent(handle(matcherMonth2.group(2)));
            } else {
                result.setContent(handle(content2));
            }
            result.setYear1(year1);
            result.setYear2(year2);
        } else if (matcher1year.find()) {
            String year1 = matcher1year.group(1);
            String content1 = matcher1year.group(3);
            Matcher matcherMonth1 = patternMonth.matcher(content1);

            if (matcherMonth1.find()) {
                result.setMonth1(matcherMonth1.group(1));
                result.setContent(handle(matcherMonth1.group(2)));
            } else {
                result.setContent(handle(content1));
            }
            result.setYear1(year1);
        } else {
            System.out.println("没匹配到");
        }
        return result;
    }

    public static String handle(String s) {
        if (s.startsWith("年") || s.startsWith("月")) {
            s = s.substring(1).trim();
        }
        Pattern pattern = Pattern.compile("^(\\d+)(.+)");
        Matcher matcher = pattern.matcher(s);
        if (matcher.find()) {
            s = matcher.group(2);
        }
        if (s.startsWith("日")) {
            s = s.substring(1).trim();
        }
        if (s.startsWith(",") || s.startsWith(" ")) {
            s = s.substring(1).trim();
        }
        return s;
    }

    static class Result {
        private String year1;
        private String month1;
        private String year2;
        private String month2;
        private String content;

        public String getYear1() {
            return year1;
        }

        public void setYear1(String year1) {
            this.year1 = year1;
        }

        public String getMonth1() {
            return month1;
        }

        public void setMonth1(String month1) {
            this.month1 = month1;
        }

        public String getYear2() {
            return year2;
        }

        public void setYear2(String year2) {
            this.year2 = year2;
        }

        public String getMonth2() {
            return month2;
        }

        public void setMonth2(String month2) {
            this.month2 = month2;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }

        @Override
        public String toString() {
            return "Result{" +
                    "year1='" + year1 + '\'' +
                    ", month1='" + month1 + '\'' +
                    ", year2='" + year2 + '\'' +
                    ", month2='" + month2 + '\'' +
                    ", content='" + content + '\'' +
                    '}';
        }
    }
}

 

posted @ 2022-09-19 11:01  一只韭菜  阅读(114)  评论(0编辑  收藏  举报