C# 提取身份證號


思路:如果號碼是18位的,並且符合生日規則,還有前面和后面都不是數字的這種才是身份證號,避免誤采

            string idCard = "  135412198801245521af888888888888888888,110444199601012145";
            idCard = " " + idCard + " ";
            MatchCollection mcIdCard = Regex.Matches(idCard, @"(\D[1-9]\d{5}[12]\d{3}(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])\d{3}[0-9xX]\D)");
            foreach (Match match in mcIdCard)
            {
                var result = match.Value;
                result = GetRealIdCardNumber(result);
                Console.WriteLine(result);
            }
            Console.ReadLine();

 

        public static string GetRealIdCardNumber(string input)
        {
            var result = string.Empty;
            if (!string.IsNullOrWhiteSpace(input))
            {
                input = input.Replace(" ", "");
                List<string> list = new List<string>();
                Regex regex = new Regex(@"([1-9]\d{5}[12]\d{3}(0[1-9]|1[012])(0[1-9]|[12][0-9]|3[01])\d{3}[0-9xX])");
                MatchCollection collection = regex.Matches(input);

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM