C#_正则表达式
2015-11-17 16:41
519 查看
//private static bool IsEmailOrNot(string email) //{ // if (email.Contains('@') && email.Contains('.')) // { // if (email.StartsWith("@") || email.StartsWith(".") || email.EndsWith("@") || email.EndsWith(".")) // { // return false; // } // else // { // if (email.LastIndexOf('@') < email.LastIndexOf('.')) // { // return true; // } // else // { // return false; // } // } // } // else return false; //}
判断邮件
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace _006正则练习 { class Program { static void Main(string[] args) { //bool b1 = Regex.IsMatch("bbbbg", "^b.*g$"); //true //bool b2 = Regex.IsMatch("bg","^b.*g$"); //true //bool b3 = Regex.IsMatch("gege","^b.*g$"); //false //Console.WriteLine("{0} {1} {2}",b1,b2,b3); #region 判断邮政编码 //while (true) //{ // Console.WriteLine("Please input:"); // string postCode = Console.ReadLine(); // bool b = Regex.IsMatch(postCode,"^[0-9]{6}$"); // bool b2 = Regex.IsMatch(postCode,"^\\d{6}$"); // //默认.net 采用 Unicode字符匹配 中文数字和英文数字匹配 // bool b3 = Regex.IsMatch(postCode, @"^\d{6}$",RegexOptions.ECMAScript); //使用RegexOptions.ECMAScript选项后 就不匹配中文数字了 // Console.WriteLine(b); //} #endregion #region 假设 1.18位都是数字 或者前17位为数字,最后为x或者X 2.15位都是数字 //while (true) //{ // Console.WriteLine("Please Input a number:"); // string s = Console.ReadLine(); // bool b = Regex.IsMatch(s, "^([0-9]{18})$|^([0-9]{17}[xX])$|^([0-9]{15})$"); // bool b2 = Regex.IsMatch(s, "^([0-9]{15}|[0-9]{17}[0-9xX])$"); // bool b3 = Regex.IsMatch(s,"^[0-9]{15}([0-9]{2}[0-9xX])?$"); // Console.WriteLine(b); //} #endregion #region 区号-电话 1. 区号3位 或4位 2.电话7位或8位 3.-可有可无 4.所有的5为电话号 5.所有的11位电话号 //while (true) //{ // Console.WriteLine("Please Input a number:"); // string s = Console.ReadLine(); // bool b = Regex.IsMatch(s, @"^(\d{3}-?\d{8}|\d{4}-?\d{7}|\d{5}|\d{11})$"); // bool b2 = Regex.IsMatch(s, @"^(\d{3,4}\-?\d{7,8}|\d{5})$", RegexOptions.ECMAScript); //} #endregion #region 邮件地址 //while (true) //{ // Console.WriteLine("Please Input you email:"); // string email = Console.ReadLine(); // //.在[] 中表示普通的. 不需要转义 - 需要转义 // bool b=Regex.IsMatch(email,@"^[a-zA-Z0-9._\-]+@[0-9a-zA-Z\-]+(\.[0-9a-zA-Z]){1,}$"); // bool b2 = Regex.IsMatch(email,@"^\w+@\w+(\.\w+){1,}$",RegexOptions.ECMAScript); //} #endregion #region 验证ip 4段: 分割最多三位数字 //192.168.15.12 333.333.55.335 //while (true) //{ // Console.WriteLine("Please input a ip Address:"); // string ip = Console.ReadLine(); // bool b = Regex.IsMatch(ip, @"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$"); // Console.WriteLine(b); //} #endregion #region 判断日期是否合法四位数字-两位数字-两位数字 while (true) { Console.WriteLine("Please input a date:"); string date = Console.ReadLine(); bool b = Regex.IsMatch(date, @"^\d{4}\-(0[1-9]|1[0-2])\-([0-2][])$"); } #endregion } } }
相关文章推荐
- (C#基础)反射理解
- C#读写txt文件的两种方法介绍
- C#编程实现取整和取余的方法
- c# 监听文件夹动作
- C#中类型转换方法
- C# 中的委托和事件 观察者模式
- C#String.Format大全
- C#编程实现四舍五入、向上及下取整的方法
- C#循环练习
- 适配器模式 C#
- C#_序列化
- C# Attributes(特性)
- Repeater 此属性不存在
- C# 将PDF文件转换为word格式
- C# 将PDF文件转换为word格式
- C# 使用Vlc播放视频或者监控
- C#关于DateTime得到的当前时间的格式和用法
- C#支付宝扫码支付代码完整版
- C# 用Attribute实现AOP事务
- (转)C# 选择正确的集合