当前位置: 移动技术网 > IT编程>开发语言>正则 > C# 正则表达式经典分类整理集合手册第1/3页

C# 正则表达式经典分类整理集合手册第1/3页

2017年12月12日  | 移动技术网IT编程  | 我要评论
有一段时间,正则表达式学习很火热很潮流,当时在csdn一天就能看到好几个正则表达式的帖子,那段时间借助论坛以及wrox press出版的《c#字符串和正则表达式参考手册》学习了一些基础的知识,同时也为我在csdn大概赚了1000分,今天想起来,去找《c#字符串和正则表达式参考手册》时,已经不知所踪了。
(1)“@”符号
符下两ows表研究室的火热,当晨在“@”虽然并非c#正则表达式的“成员”,但是它经常与c#正则表达式出双入对。“@”表示,跟在它后面的字符串是个“逐字字符串”,不是很好理解,举个例子,以下两个声明是等效的:
string x="d:\\my huang\\my doc";
string y = @"d:\my huang\my doc";
事实上,如果按如下声明,c#将会报错,因为“\”在c#中用于实现转义,如“\n”换行:
string x = "d:\my huang\my doc";

(2)基本的语法字符。
\d 0-9的数字
\d \d的补集(以所以字符为全集,下同),即所有非数字的字符
\w 单词字符,指大小写字母、0-9的数字、下划线
\w \w的补集
\s 空白字符,包括换行符\n、回车符\r、制表符\t、垂直制表符\v、换页符\f
\s \s的补集
. 除换行符\n外的任意字符
[…] 匹配[]内所列出的所有字符
[^…] 匹配非[]内所列出的字符
下面提供一些简单的示例:
复制代码 代码如下:

string i = "\n";
string m = "3";
regex r = new regex(@"\d");
//同regex r = new regex("\\d");
//r.ismatch(i)结果:true
//r.ismatch(m)结果:false

string i = "%";
string m = "3";
regex r = new regex("[a-z0-9]");
//匹配小写字母或数字字符
//r.ismatch(i)结果:false
//r.ismatch(m)结果:true

3)定位字符
“定位字符”所代表的是一个虚的字符,它代表一个位置,你也可以直观地认为“定位字符”所代表的是某个字符与字符间的那个微小间隙。
^ 表示其后的字符必须位于字符串的开始处
$ 表示其前面的字符必须位于字符串的结束处
\b 匹配一个单词的边界
\b 匹配一个非单词的边界
另外,还包括:\a 前面的字符必须位于字符处的开始处,\z 前面的字符必须位于字符串的结束处,\z 前面的字符必须位于字符串的结束处,或者位于换行符前
下面提供一些简单的示例:
复制代码 代码如下:

string i = "live for nothing,die for something";
regex r1 = new regex("^live for nothing,die for something$");
//r1.ismatch(i) true
regex r2 = new regex("^live for nothing,die for some$");
//r2.ismatch(i) false
regex r3 = new regex("^live for nothing,die for some");
//r3.ismatch(i) true

string i = @"live for nothing,
die for something";//多行
regex r1 = new regex("^live for nothing,die for something$");
console.writeline("r1 match count:" + r1.matches(i).count);//0
regex r2 = new regex("^live for nothing,die for something$", regexoptions.multiline);
console.writeline("r2 match count:" + r2.matches(i).count);//0
regex r3 = new regex("^live for nothing,\r\ndie for something$");
console.writeline("r3 match count:" + r3.matches(i).count);//1
regex r4 = new regex("^live for nothing,$");
console.writeline("r4 match count:" + r4.matches(i).count);//0
regex r5 = new regex("^live for nothing,$", regexoptions.multiline);
console.writeline("r5 match count:" + r5.matches(i).count);//0
regex r6 = new regex("^live for nothing,\r\n$");
console.writeline("r6 match count:" + r6.matches(i).count);//0
regex r7 = new regex("^live for nothing,\r\n$", regexoptions.multiline);
console.writeline("r7 match count:" + r7.matches(i).count);//0
regex r8 = new regex("^live for nothing,\r$");
console.writeline("r8 match count:" + r8.matches(i).count);//0
regex r9 = new regex("^live for nothing,\r$", regexoptions.multiline);
console.writeline("r9 match count:" + r9.matches(i).count);//1
regex r10 = new regex("^die for something$");
console.writeline("r10 match count:" + r10.matches(i).count);//0
regex r11 = new regex("^die for something$", regexoptions.multiline);
console.writeline("r11 match count:" + r11.matches(i).count);//1
regex r12 = new regex("^");
console.writeline("r12 match count:" + r12.matches(i).count);//1
regex r13 = new regex("$");
console.writeline("r13 match count:" + r13.matches(i).count);//1
regex r14 = new regex("^", regexoptions.multiline);
console.writeline("r14 match count:" + r14.matches(i).count);//2
regex r15 = new regex("$", regexoptions.multiline);
console.writeline("r15 match count:" + r15.matches(i).count);//2
regex r16 = new regex("^live for nothing,\r$\n^die for something$", regexoptions.multiline);
console.writeline("r16 match count:" + r16.matches(i).count);//1
//对于一个多行字符串,在设置了multiline选项之后,^和$将出现多次匹配。

string i = "live for nothing,die for something";
string m = "live for nothing,die for some thing";
regex r1 = new regex(@"\bthing\b");
console.writeline("r1 match count:" + r1.matches(i).count);//0
regex r2 = new regex(@"thing\b");
console.writeline("r2 match count:" + r2.matches(i).count);//2
regex r3 = new regex(@"\bthing\b");
console.writeline("r3 match count:" + r3.matches(m).count);//1
regex r4 = new regex(@"\bfor something\b");
console.writeline("r4 match count:" + r4.matches(i).count);//1
//\b通常用于约束一个完整的单词

1

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网