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;
            }
        }
    }
  • 相关阅读:
    FBX Software Development Kit
    如何用Nsight调试C# OpenGL程序
    Xcode如何编译Debug版和Release版
    Mapbox
    OpenGL
    OpenGL Tutorial
    Unity3D Sprite Draw Call合批规则
    MONO加载DLL调试命令
    如何在Flash Builder里新建ActionScript工程
    Flash Frameworks
  • 原文地址:https://www.cnblogs.com/wjx-blog/p/15380766.html
Copyright © 2011-2022 走看看