c# 提取手机号

时间:2021-10-08
本文章向大家介绍c# 提取手机号,主要包括c# 提取手机号使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

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

static void Main(string[] args)
        {
            string strResult = " 132 4447 8896";
            strResult = " " + strResult + " ";
            Console.WriteLine(string.Join(",", GetTelephoneList(strResult)));
        }
        public static List<string> GetTelephoneList(string input)
        {
            List<string> list = new List<string>();
            Regex regex = new Regex(@"(\D1[3|4|5|6|7|8|9]\d{9}\D)|(\D1[3|4|5|6|7|8|9]\d \d\d\d\d \d\d\d\d\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;
        }

原文地址:https://www.cnblogs.com/wjx-blog/p/15380766.html