您的位置:首页 > 其它

.NET 正则表达式简单语法(初学者)

2011-02-24 13:50 357 查看

接触正则表达式不久,现在只能Copy一些常见语法,以及了解简单的语法分析,今天有时间,将收集的语法整理了一下,并简单的分析!

//首先要引用该类:

using System.Text.RegularExpressions;

string str = "^[1-9]$|^[1-9][0-9]$";
// 以1到9任意数字开头并以1到9任意数字结尾的一位数 或者 以1到9任意数字开头并以0到9任意数字结尾
的两位数

string str = @"^/d{3}$";
// "/d"表示数字,以数字开头并以数字结尾,数字必须出现3次

string str = @"^/d{2,5}$";
// 数字至少出现2次,最多出现5次

string str = @"^/d{3,}$";
// 数字至少出现3次

string str = @"^/d{1,2}/w{1}$";
// /w表示字母,以数字开头,以字母结尾,并且,数字至少出现1次,最多出现2次,字母必须出现一次

string str = @"^http(s)?://([/w-]+/.)+[/w-]+(/[/w- ./?%&=]*)?$";
// 一般表示网址

string str = @"^(/(/d{3}/)|/d{3}-)?/d{8}$";
// 一般表示电话号码

string str = @"^/d{17}[/d|X]|/d{15}$";
// 一般表示身份证

string str = @"^/w+([-+.']/w+)*@/w+([-.]/w+)*/./w+([-.]/w+)*$";
// 一般表示电子邮件

string str = @"^((2[0-4]/d|25[0-5]|[01]?/d/d?)/.){3}(2[0-4]/d|25[0-5]|[01]?/d/d?)$";

string str = @"^(<(2[0-4]/d(?#200-249)|25[0-5](?#250-255)|[01]?/d/d?(?#0-199))>/.)*/1$";

//Ip地址表示语法,#表示注释

//创建正则表达式的类:

Regex regex = new Regec(str);

//创建正则表达式匹配的类:

Match result = regex.Match(txtValue.Text);

//判断是否匹配

if (result.Success)
MessageBox.Show("匹配成功");
else
MessageBox.Show("失败");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: