找出文本中的多个手机号码

 /// <summary>
        /// 从一段文本中过滤出手机号,如"地址是在燕18892456564山13492456564路378号~~~洋纱小区对面 传真051253581349董事长:15133652276 1513365227615133652276总经理15133652276535753577308办公室  53577311销售010-53574182财务53577318生产53577307食品协会53577309中";
        /// </summary>
        /// <param name="strPhone"></param>
        /// <returns></returns>
        private List<string> 查询有效的手机号码(string strPhone)
        {
            List<string> lst = new List<string>();
            Regex reBlock = new Regex(@"[\d]+");
            Regex reMobile = new Regex(@"^1\d{10}$");

            MatchCollection matchList = reBlock.Matches(strPhone);
            for (int n = 0; n < matchList.Count; n++)
            {
                string str = matchList[n].Value;
                if (reMobile.Match(str).Success == true)
                {
                    lst.Add(str);
                }
            }
            return lst;
        }

 

参考:https://blog.csdn.net/ArvinStudy/article/details/7402756

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
 
namespace RegularPhoneApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string strPhone = "地址是在燕18892456564山13492456564路378号~~~洋纱小区对面 传真051253581349董事长:15133652276 1513365227615133652276总经理15133652276535753577308办公室  53577311销售010-53574182财务53577318生产53577307食品协会53577309中";
            VaildPhone(strPhone);
        }
 
        /// <summary>
        /// 查询有效的手机号码或是电话号码
        /// </summary>
        /// <param name="strPhone"></param>
        static void VaildPhone(string strPhone)
        {
            MatchCollection matchList;
            Regex reBlock=new Regex(@"(\d{4}-|\d{3}-)?[\d]+");
            Regex reMobile = new Regex(@"^1(3[4-9]|5[012789]|8[78])\d{8}$");
            Regex reUnicom = new Regex(@"^1(3[0-2]|5[56]|8[56])\d{8}$");
            Regex reTelecom = new Regex(@"^18[0-9]\d{8}$");
            Regex reCDMA = new Regex(@"^1[35]3\d{8}$");
            Regex rePhone = new Regex(@"^((\d{4}-)?\d{7}|(\d{3}-)?\d{8})$");
            matchList = reBlock.Matches(strPhone);
            ArrayList PhoneArray = new ArrayList();  
            for (int n = 0; n < matchList.Count; n++)
            {
                if (getValue(reMobile, matchList[n].Value) != "")
                {
                    PhoneArray.Add(getValue(reMobile, matchList[n].Value));
                    continue;
                }
                if (getValue(reUnicom, matchList[n].Value) != "")
                {
                    PhoneArray.Add(getValue(reUnicom, matchList[n].Value));
                    continue;
                }
                if (getValue(reTelecom, matchList[n].Value) != "")
                {
                    PhoneArray.Add(getValue(reTelecom, matchList[n].Value));
                    continue;
                }
                if (getValue(reCDMA, matchList[n].Value) != "")
                {
                    PhoneArray.Add(getValue(reCDMA, matchList[n].Value));
                    continue;
                }
                if (getValue(rePhone, matchList[n].Value) != "")
                {
                    PhoneArray.Add(getValue(rePhone, matchList[n].Value));
                    continue;
                }
            }
            foreach (var item in PhoneArray)
            {
                Console.WriteLine(item);
            }
            Console.Read();
        }
        /// <summary>
        /// 获得验证结果
        /// </summary>
        /// <param name="Reg">匹配的正则</param>
        /// <param name="Info">匹配的信息</param>
        /// <returns></returns>
        static string getValue(Regex Reg, string Info)
        {
            if (Reg.Match(Info).Success)
            {
                return Reg.Match(Info).Value;
            }
            else
            {
                return "";
            }
        }
        
    }
}

 

posted @   81  阅读(232)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
点击右上角即可分享
微信分享提示