c# 提取手机号

支持提取:13255544444或 136 7789 9654格式的手机号

 

 

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace ConsoleApp4
{
    class Program
    {
        static void Main(string[] args)
        {
            string strResult = " 132 4447 8896|13255789654";
            strResult = " " + strResult + " ";

            List<string> lstResult = new List<string>();
            lstResult.AddRange(GetTelephoneListOne(strResult));
            lstResult.AddRange(GetTelephoneListTwo(strResult));
            Console.WriteLine(string.Join(",", lstResult));
        }
        public static List<string> GetTelephoneListOne(string input)
        {
            List<string> list = new List<string>();
            Regex regex = new Regex(@"(\D1[3|4|5|6|7|8|9]\d{9}\D)");
            MatchCollection collection = regex.Matches(input);
            string telephone;
            foreach (Match item in collection)
            {
                foreach (Group group in item.Groups)
                {
                    telephone = group.Value.Trim();
                    if (!string.IsNullOrEmpty(telephone))
                    {
                        telephone = GetRealTelephoneList(telephone);
                        if (!list.Contains(telephone))
                        {
                            list.Add(telephone);
                        }
                    }
                }
            }
            return list;
        }
        public static List<string> GetTelephoneListTwo(string input)
        {
            List<string> list = new List<string>();
            Regex regex = new Regex(@"(\D1[3|4|5|6|7|8|9]\d{1} \d{4} \d{4}\D)");
            MatchCollection collection = regex.Matches(input);
            string telephone;
            foreach (Match item in collection)
            {
                foreach (Group group in item.Groups)
                {
                    telephone = group.Value.Trim();
                    if (!string.IsNullOrEmpty(telephone))
                    {
                        telephone = GetRealTelephoneList(telephone);
                        if (!list.Contains(telephone))
                        {
                            list.Add(telephone);
                        }
                    }
                }
            }
            return list;
        }
        public static string GetRealTelephoneList(string input)
        {
            var result = string.Empty;
            if (!string.IsNullOrWhiteSpace(input))
            {
                input = input.Replace(" ", "");
                List<string> list = new List<string>();
                Regex regex = new Regex(@"(1[3|4|5|6|7|8|9]\d{9})");
                MatchCollection collection = regex.Matches(input);

                if (collection.Count > 0 && collection[0].Groups.Count > 0)
                {
                    result = collection[0].Groups[0].Value;
                }
            }
            return result;
        }
    }
}

 

posted @ 2021-10-08 17:13  星星c#  阅读(729)  评论(0编辑  收藏  举报