java从字符串中提取手机号电话号码

java从字符串中提取手机号电话号码

import java.util.regex.Matcher;  
import java.util.regex.Pattern; 
import java.util.HashSet;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;


/** 从字符串中提取手机号的类
 *  本程序写正则从字符串中提取电话号码
 * 作者:李兴球
 * 日期:2018/1/10
 * QQ:406273900
 * 网址: www.scratch8.net
 * 关键词: java extract regex link 
 */

public class MobileOperation
{
    //这是一个测试,本程序从一些文本中提取网页链接.
    public static void main(String[] args) throws IOException
    {
        String someTxt = ReadTextFile("C:\\test.txt","utf-8");
        MobileOperation mb = new MobileOperation();
        //HashSet mbList = mb.extractMobile("abcd13012348321少儿编程你是人吗? 13784680991我们都是好朋友大润发张国荣刘德华");
        HashSet mbList = mb.extractMobile(someTxt);
        System.out.println(mbList);

        System.out.println(mb.containsMobile(someTxt));
          
    } 


    public HashSet extractMobile(String someTxt)
    {
        HashSet teleList=new HashSet();
        String oneMobileNumber = "";
        if (someTxt.length()>0)
        {
            //Pattern pattern = Pattern.compile("(0\\d{2}-\\d{8}(-\\d{1,4})?)|(0\\d{3}-\\d{7,8}(-\\d{1,4})?)");   //这是固定电话正则
            Pattern pattern = Pattern.compile("((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0|3|5|6|7|8|9]))\\d{8}");
            // 创建匹配给定输入与此模式的匹配器。
            Matcher matcher = pattern.matcher(someTxt); 
            //查找字符串中是否有符合的子字符串
            while (matcher.find())
            {   
                oneMobileNumber = matcher.group();
                if (oneMobileNumber.length()==11)
                   teleList.add(oneMobileNumber);
            }
          
        }
        return teleList;
       
    }

   public  boolean containsMobile(String someText)
   {
       HashSet teleList= extractMobile(someText);
       if (teleList.size()==0)
          return false;
       else
          return true;
   }

  public static String ReadTextFile(String filePath,String encodeStyle) throws IOException
  {
        //读文本文件,指定文件编码UTF-8,GB2312
        StringBuffer contents= new StringBuffer();
        String line;

        try (BufferedReader br = new BufferedReader(new FileReader(filePath)))
        {
           while ((line = br.readLine()) != null)
           {
                contents.append(line);
            }
         } catch (IOException e) {   e.printStackTrace();    }
        String result=new String(contents.toString().getBytes(),encodeStyle);
        return result;


  }


}

李兴球

李兴球的博客是Python创意编程原创博客