C# 正则提取18位身份证号码

2023-03-30  本文已影响0人  Rinaloving

正则提取18位身份证号码

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;
        }
上一篇 下一篇

猜你喜欢

热点阅读