java 正则表达式的应用:读取文件,获取其电话号码

完整代码如下:

    package 读大文件查找;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Set;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    
    
    /**
     * 读取文件操作
     * 
     * @author zcr
     * 
     */
    public class BigFileMain
    {
        /**
         * 读取文件,将文件中的电话号码读取出来,保存在Set中。
         * @param filePath    文件的绝对路径
         * @return            文件中包含的电话号码
         */
    	
    	public static void main(String[] args)
        {
            String filePath = "F:\\three.txt";    
            
            Set<String> phoneSet = getPhoneNumFromFile(filePath);
            
            System.out.println("电话集合:" + phoneSet);
        }
    	
        public static Set<String> getPhoneNumFromFile(String filePath)
        {
            Set<String> phoneSet = new HashSet<String>();
            
            try
            {
                String encoding = "UTF-8";
                File file = new File(filePath);
                if (file.isFile() && file.exists())
                { // 判断文件是否存在
                    InputStreamReader read = new InputStreamReader(
                            new FileInputStream(file), encoding);// 考虑到编码格
                    BufferedReader bufferedReader = new BufferedReader(read);
                    String lineTxt = null;
    
                    while ((lineTxt = bufferedReader.readLine()) != null)
                    {
                    	
                    	//读取文件中的一行,将其中的电话号码添加到phoneSet中   
                        CheckIfIsPhoneNumber.getPhoneNumFromStrIntoSet(lineTxt, phoneSet);
                    }
                    read.close();
                }
                else
                {
                    System.out.println("找不到指定的文件");
                }
            }
            catch (Exception e)
            {
                System.out.println("读取文件内容出错");
                e.printStackTrace();
            }
            
            return phoneSet;
        }
    
    }
    class CheckIfIsPhoneNumber 
    {
        
        /**
         * 获得电话号码的正则表达式:包括固定电话和移动电话
         * 符合规则的号码:
         *     1》、移动电话
         *         86+‘-’+11位电话号码
         *         86+11位正常的电话号码
         *         11位正常电话号码a
         *         (+86) + 11位电话号码
         *         (86) + 11位电话号码
         *     2》、固定电话
         *         区号 + ‘-’ + 固定电话  + ‘-’ + 分机号
         *         区号 + ‘-’ + 固定电话 
         *         区号 + 固定电话
         * @return    电话号码的正则表达式
         */
        public static String isPhoneRegexp()
        {
            String regexp = "";
            
            //能满足最长匹配,但无法完成国家区域号和电话号码之间有空格的情况
            String mobilePhoneRegexp = "(?:(\\(\\+?86\\))((13[0-9]{1})|(15[0-9]{1})|(18[0,5-9]{1}))+\\d{8})|" +     
                    "(?:86-?((13[0-9]{1})|(15[0-9]{1})|(18[0,5-9]{1}))+\\d{8})|" +
                    "(?:((13[0-9]{1})|(15[0-9]{1})|(18[0,5-9]{1}))+\\d{8})";
            
            
            
            //    System.out.println("regexp = " + mobilePhoneRegexp);
            //固定电话正则表达式
            
            String landlinePhoneRegexp = "(?:(\\(\\+?86\\))(0[0-9]{2,3}\\-?)?([2-9][0-9]{6,7})+(\\-[0-9]{1,4})?)|" +
                    "(?:(86-?)?(0[0-9]{2,3}\\-?)?([2-9][0-9]{6,7})+(\\-[0-9]{1,4})?)";    
    
            regexp += "(?:" + mobilePhoneRegexp + "|" + landlinePhoneRegexp +")"; 
        
            return regexp;
        }
        
        
        /**
         * 从dataStr中获取出所有的电话号码(固话和移动电话),将其放入Set
         * @param dataStr    待查找的字符串
         * @param phoneSet    dataStr中的电话号码
         */
        public static void getPhoneNumFromStrIntoSet(String dataStr,Set<String> phoneSet)
        {
            //获得固定电话和移动电话的正则表达式
            String regexp = isPhoneRegexp();
            
            System.out.println("Regexp = " + regexp);
            
            Pattern pattern = Pattern.compile(regexp); 
            Matcher matcher = pattern.matcher(dataStr); 
    
            //找与该模式匹配的输入序列的下一个子序列
            while (matcher.find()) 
            { 
                //获取到之前查找到的字符串,并将其添加入set中
                phoneSet.add(matcher.group());
            } 
            //System.out.println(phoneSet);
        }
    } 

更多推荐