• 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;
            }
        }
    }
  • 相关阅读:
    老婆和老媽同時掉在了水裡终于有答案了
    js对象序列化为json字符串
    基于jQuery的单据输入
    重发我的 HTML单据输入控件
    Spring MVC 实现REST风格API版本控制
    匿名方法和Lambda 表达式
    委托的定义和委托的实例化和使用
    LINQ进阶
    C#3.0的新特性
    使用 var 和 object 声明变量有什么区别?
  • 原文地址:https://www.cnblogs.com/wjx-blog/p/15380766.html
Copyright © 2020-2023  润新知