每当我们写信的时候,都需要再信封填写邮政编号,邮政编号是有六个数字组成,每个地区的邮政编号都不相同,下面,济南网站建设小编就来与大家分享,通过正则表达式简单实现判断邮政编号是否合规,有兴趣的朋友可以过来一起关注一下。
关键代码:
using System; using System.Text.RegularExpressions; class Program { static void Main() { // 定义用于匹配中国邮政编码的正则表达式 string pattern = @"^\d{6}$"; Regex regex = new Regex(pattern); Console.WriteLine("您好!请输入一个中国邮政编号:"); string input = Console.ReadLine(); if (regex.IsMatch(input)) { Console.WriteLine("您输入的邮政编号是:{0},恭喜,邮政编号格式正确!",input); } else { Console.WriteLine("您输入的邮政编号是:{0},抱歉,邮政编号格式有误,请检查并重试。",input); } } }
评论