c#.net使用正则表达式验证文本中是否包含手机号码或电话号码?

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);
        }

        /// 
        /// 查询有效的手机号码或是电话号码
        /// 
        /// 
        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();
        }
        /// 
        /// 获得验证结果
        /// 
        /// 匹配的正则
        /// 匹配的信息
        /// 
        static string getValue(Regex Reg, string Info)
        {
            if (Reg.Match(Info).Success)
            {
                return Reg.Match(Info).Value;
            }
            else
            {
                return "";
            }
        }
        
    }
}


你可能感兴趣的:(█小小代码,正则表达式,C#基础知识)