您的位置:首页 > 编程语言 > C#

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

}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: