java正则:获取文本中符合特定格式的数据的集合,并实现两个文本的比较

需求背景:

shops.java文件中@Field("shoptypeId")中的字段和schemal.xml文件中的字段,存在大小写错误的情况,需核实
 

package com.wpao.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * shops.java文件中@Field("shoptypeId")中的字段和schemal.xml文件中的字段,存在大小写错误的情况,需核实
 *     分别读入两个文件符合格式的字符串集合
 *     对比两个集合查找不同的地方
 * @author dada
 *
 */
public class FileRegexTools {
    
    /**
     * 按行读取文件,并提取特定格式中间的数据
     * @param filePath
     * @param regex
     * @return
     */
    public static List fileRegex(String filePath, String regex){
        List list = new ArrayList (200);
        Pattern pattern = Pattern.compile(regex);// 匹配的模式
        Matcher m = null;
        String line = null;
        String result = null;
        BufferedReader buffer = null;
        try {
            buffer = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "utf-8"));
            while((line = buffer.readLine()) != null){
                line = line.trim().replace("(", "#").replace(")", "#"); //处理文本中的正则字符串
                //System.out.println(line);
                m = pattern.matcher(line);
                while(m.find()){
                    result = m.group(1);
                    //System.out.println(result);
                    list.add(result);
                }
                
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(buffer != null){
                try {
                    buffer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return list;
        
    }
    
    /**
     * 用于测试正则匹配情况
     * @param str
     * @param regex
     */
    public static void checkRegex(String str,String regex){
        Pattern pattern = Pattern.compile(regex);
        Matcher m = null;
        m = pattern.matcher(str);
        System.out.print("                  str:["+str+"]  ,regex:["+regex+"]");
        while(m.find()){
            System.out.println(" ,匹配的结果:"+m.group(1));
        }
    }
    
    public static void main(String[] args) {
        
        FileRegexTools.checkRegex("@Field#\"spType\"#", "@Field#\"(.*?)\"#");
        List list = FileRegexTools.fileRegex("D:\\test\\Shops-liyd-end.java", "@Field#\"(.*?)\"#");
        
        FileRegexTools.checkRegex("         List list2 = FileRegexTools.fileRegex("D:\\test\\lyd_schema.txt", "         
        for (String string1 : list) {
            boolean isexist = false;
            for (String string2 : list2) {
                if(string1.equalsIgnoreCase(string2)){
                    isexist = true;
                    if(!string1.equals(string2)){
                        System.out.println(string1+"-----"+string2);
                    }
                }
            }
            if(!isexist){
                System.out.println("集合1中的字符串在2中不存在:"+string1);
            }
        }
    }

}

 

你可能感兴趣的:(工具类)