【Excel】【正则】判断手机号是否正确

发现导出的手机号,有部分存在问题,故想直接通过excel做正则的判断

先说结论:通过VBA实现了正则函数的嵌入,但实际效率非常低下,80万的数据跑了半小时(T14 十代i7)

参考文章:正则表达式直接在EXCEL中使用的详细步骤_正则表达式_脚本之家

第一步,找到VAB的函数(百度找的,找不到来源了)

Public Function RegExpMatch(input_range As Range, pattern As String, Optional match_case As Boolean = True) As Variant

   '存储结果的数组

    Dim arRes() As Variant

   '源单元格区域中当前行索引值

   '源单元格区域中当前列索引值

   '行数, 列数

    Dim iInputCurRow As Long

    Dim iInputCurCol As Long

    Dim cntInputRows As Long

    Dim cntInputCols As Long

    On Error GoTo ErrHandl

    RegExpMatch = arRes

   

    Set regex = CreateObject("VBScript.RegExp")

   

   regex.pattern = pattern

   regex.Global = True

   regex.MultiLine = True

   

    If True = match_case Then

       regex.ignorecase = False

    Else

       regex.ignorecase = True

    End If

   

   cntInputRows = input_range.Rows.Count

   cntInputCols = input_range.Columns.Count

    ReDim arRes(1 To cntInputRows, 1 To cntInputCols)

 

    For iInputCurRow = 1 To cntInputRows

        For iInputCurCol = 1 To cntInputCols

           arRes(iInputCurRow, iInputCurCol) = regex.Test(input_range.Cells(iInputCurRow, iInputCurCol).Value)

        Next

    Next

 

  RegExpMatch = arRes

  Exit Function

 

ErrHandl:

   RegExpMatch = CVErr(xlErrValue)

End Function

第二步,嵌入到Excel中,顺序如下:

        

下面我们使用的这个方法,定义出的函数将长期有效:

1、新建一个EXCEL文件,我这里命名为RE,随后按ALT+F11打开宏编辑器,选中任意一个sheet,右键,选择插入模块:

2、双击模块1,编辑如第一步的VBA自定义函数代码

3、另存为加载宏格式:

4、点击下方的加载项:(不同版本Excel可以百度如何打开开发工具以及如何打开加载项)

5、点击浏览:

6、选择我们刚保存的加载宏格式文件,结果如下图:

好了,到这里我们的正则函数就创建好了,随后每次打开EXCEL都可以直接使用定义的RegExpMatch函数,按照需求来敲出合适的正则表达式。

第三步,找一段手机号匹配的正则规则

^((\+?86)|(\(\+86\)))?1[3-9]\d{9}$

#匹配前缀是86或者+86,第一位为1,第二位为3-9的数字,第三位开始有9位数字#

第四步,到excel使用函数

=RegExpMatch(A1,"^((\+?86)|(\(\+86\)))?1[3-9]\d{9}$")

最终,对80万手机号进行了验证,花了大概半小时,而且每次打开都要重新跑一次(不过会快一点),建议跑完之后立即将结果复制成文本。

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
/** * 此代码是完成从excel导入电话号码,将正确电话号码保存到set集合中,因为set集合对于重复的值会覆盖,所以达到了去重复的值的用例,并累计了不正确电话号码的个数,对电话号码进行了验证有效性。所需要的 dom4j-1.6.1.jar;geronimo-stax-api_1.0_spec-1.0.jar;poi-3.7-20101029.jar;poi-ooxml-3.7-20101029.jar;poi-ooxml-schemas-3.7-20101029.jar;xmlbeans-2.3.0.jar; */ public static void main(String[] args) { Long errorMobileTotal=0L; // 保存正确电话号码 Set<String> mobileSet = new HashSet<String>(); try { XSSFWorkbook wb = new XSSFWorkbook("E:/workbook1.xlsx"); XSSFSheet sheet = wb.getSheetAt(0); XSSFRow row = null; XSSFCell cell = null; String mobileStr=""; for (int i = 0; i <= sheet.getLastRowNum(); i++) { row = sheet.getRow(i); //System.out.print("第" + i + "行共" + row.getLastCellNum() +"列: "); for (int y = 0; y < row.getLastCellNum(); y++) { cell = row.getCell(y); // 设置字段为字符类型 cell.setCellType(XSSFCell.CELL_TYPE_STRING); // 判断储存格的格式 if (cell != null) { // 取得单元格的值 mobileStr = cell.getStringCellValue(); // 对手机号码进行验证身份正确 if(isMobileNO(mobileStr)) { // 保存正确手机号码 mobileSet.add(mobileStr); System.out.println("号码"+mobileStr+"正确"); } else { // 累计不正确电话号码的个数 errorMobileTotal++; System.out.println("不正确电话号码个数:"+errorMobileTotal); System.out.println("号码"+mobileStr+"不正确"); } } // end (cell != null) }// end 遍历当前行 } // end 遍历当前工作单元sheet System.out.println("总共的行数:"+ (Long.valueOf(sheet.getLastRowNum())+1)); } catch (Exception e) { e.printStackTrace(); } // 因为要去除重复的所以可能有存在替换的字符 System.out.println("不正确电话号码个数:"+errorMobileTotal); System.out.println("正确电话号码个数:" + mobileSet.size()); } public static boolean isMobileNO(String mobiles){ Pattern p = Pattern.compile("^(\\+86)*0*((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$"); Matcher m = p.matcher(mobiles); return m.matches(); }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值