zoukankan      html  css  js  c++  java
  • 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;
            }
        }
    }
  • 相关阅读:
    html中的背景图片不能充满整个浏览器 .
    linux系统安装Memcache
    linux下如何查看某软件是否已安装
    Redis监控之redisstat安装与详解
    memcached出现:Fatal error: Call to undefined method Memcached::connect()
    PHP多进程开发与Redis结合实践
    关于大型asp.net应用系统的架构架构的选择(转载)
    浅谈https\ssl\数字证书
    利用Lucene.net对附件做搜索(转载)
    Mic's blog iis下ISAPI_Rewrite配置及 iis rewrite 规则书写
  • 原文地址:https://www.cnblogs.com/wjx-blog/p/15380766.html
Copyright © 2011-2022 走看看