当前位置: 移动技术网 > IT编程>开发语言>c# > 实例详解C#正则表达式

实例详解C#正则表达式

2019年07月18日  | 移动技术网IT编程  | 我要评论

有一段时间,正则表达式学习很火热很潮流,当时在脚本之间平台一天就能看到好几个正则表达式的帖子,那段时间借助论坛以及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 = "";
 regex r = new regex(@"/d");
 //同regex r = new regex("//d");
 //r.ismatch(i)结果:true
 //r.ismatch(m)结果:false
 string i = "%";
 string m = "";
 regex r = new regex("[a-z-]");
 //匹配小写字母或数字字符
 //r.ismatch(i)结果:false
 //r.ismatch(m)结果:true

(3)定位字符

“定位字符”所代表的是一个虚的字符,它代表一个位置,你也可以直观地认为“定位字符”所代表的是某个字符与字符间的那个微小间隙。

^  表示其后的字符必须位于字符串的开始处
$  表示其前面的字符必须位于字符串的结束处
/b  匹配一个单词的边界
/b  匹配一个非单词的边界

另外,还包括:/a  前面的字符必须位于字符处的开始处,/z  前面的字符必须位于字符串的结束处,/z  前面的字符必须位于字符串的结束处,或者位于换行符前

下面提供一些简单的示例:

string i = "live for nothing,die for something";
 regex r = new regex("^live for nothing,die for something$");
 //r.ismatch(i) true
 regex r = new regex("^live for nothing,die for some$");
 //r.ismatch(i) false
 regex r = new regex("^live for nothing,die for some");
 //r.ismatch(i) true
 string i = @"live for nothing,
 die for something";//多行
 regex r = new regex("^live for nothing,die for something$");
 console.writeline("r match count:" + r.matches(i).count);//
 regex r = new regex("^live for nothing,die for something$", regexoptions.multiline);
 console.writeline("r match count:" + r.matches(i).count);//
 regex r = new regex("^live for nothing,/r/ndie for something$");
 console.writeline("r match count:" + r.matches(i).count);//
 regex r = new regex("^live for nothing,$");
 console.writeline("r match count:" + r.matches(i).count);//
 regex r = new regex("^live for nothing,$", regexoptions.multiline);
 console.writeline("r match count:" + r.matches(i).count);//
 regex r = new regex("^live for nothing,/r/n$");
 console.writeline("r match count:" + r.matches(i).count);//
 regex r = new regex("^live for nothing,/r/n$", regexoptions.multiline);
 console.writeline("r match count:" + r.matches(i).count);//
 regex r = new regex("^live for nothing,/r$");
 console.writeline("r match count:" + r.matches(i).count);//
 regex r = new regex("^live for nothing,/r$", regexoptions.multiline);
 console.writeline("r match count:" + r.matches(i).count);//
 regex r = new regex("^die for something$");
 console.writeline("r match count:" + r.matches(i).count);//
 regex r = new regex("^die for something$", regexoptions.multiline);
 console.writeline("r match count:" + r.matches(i).count);//
 regex r = new regex("^");
 console.writeline("r match count:" + r.matches(i).count);//
 regex r = new regex("$");
 console.writeline("r match count:" + r.matches(i).count);//
 regex r = new regex("^", regexoptions.multiline);
 console.writeline("r match count:" + r.matches(i).count);//
 regex r = new regex("$", regexoptions.multiline);
 console.writeline("r match count:" + r.matches(i).count);//
 regex r = new regex("^live for nothing,/r$/n^die for something$", regexoptions.multiline);
 console.writeline("r match count:" + r.matches(i).count);//
 //对于一个多行字符串,在设置了multiline选项之后,^和$将出现多次匹配。
 string i = "live for nothing,die for something";
 string m = "live for nothing,die for some thing";
 regex r = new regex(@"/bthing/b");
 console.writeline("r match count:" + r.matches(i).count);//
 regex r = new regex(@"thing/b");
 console.writeline("r match count:" + r.matches(i).count);//
 regex r = new regex(@"/bthing/b");
 console.writeline("r match count:" + r.matches(m).count);//
 regex r = new regex(@"/bfor something/b");
 console.writeline("r match count:" + r.matches(i).count);//
 ///b通常用于约束一个完整的单词

(4)重复描述字符

“重复描述字符”是体现c#正则表达式“很好很强大”的地方之一:

{n}  匹配前面的字符n次
{n,}  匹配前面的字符n次或多于n次
{n,m}  匹配前面的字符n到m次
?  匹配前面的字符0或1次
+  匹配前面的字符1次或多于1次
*  匹配前面的字符0次或式于0次

以下提供一些简单的示例:

string x = "";
 string y = "+";
 string z = ",";
 string a = "";
 string b="-";
 string c = "";
 regex r = new regex(@"^/+?[-],?/d{}$");
 console.writeline("x match count:" + r.matches(x).count);//
 console.writeline("y match count:" + r.matches(y).count);//
 console.writeline("z match count:" + r.matches(z).count);//
 console.writeline("a match count:" + r.matches(a).count);//
 console.writeline("b match count:" + r.matches(b).count);//
 console.writeline("c match count:" + r.matches(c).count);//
 //匹配到的整数。
 //http://www.cnblogs.com/sosoft/

(5)择一匹配

c#正则表达式中的 (|) 符号似乎没有一个专门的称谓,姑且称之为“择一匹配”吧。事实上,像[a-z]也是一种择一匹配,只不过它只能匹配单个字符,而(|)则提供了更大的范围,(ab|xy)表示匹配ab或匹配xy。注意“|”与“()”在此是一个整体。下面提供一些简单的示例: 

 string x = "";
 string y = ".";
 string z = "";
 string a = ".";
 string b = ".";
 string c = ".";
 string d = ".";
 string e = ".";
 regex r = new regex(@"^/+?(((.+)*)|([-]?[-])(/./d+)*)$");
 console.writeline("x match count:" + r.matches(x).count);//
 console.writeline("y match count:" + r.matches(y).count);//
 console.writeline("z match count:" + r.matches(z).count);//
 console.writeline("a match count:" + r.matches(a).count);//
 console.writeline("b match count:" + r.matches(b).count);//
 console.writeline("c match count:" + r.matches(c).count);//
 console.writeline("d match count:" + r.matches(d).count);//
 console.writeline("e match count:" + r.matches(e).count);//
 //匹配到的数

最外层的括号内包含两部分“(100(.0+)*)”,“([1-9]?[0-9])(/./d+)*”,这两部分是“or”的关系,即正则表达式引擎会先尝试匹配100,如果失败,则尝试匹配后一个表达式(表示[0,100)范围中的数字)。

(6)特殊字符的匹配

下面提供一些简单的示例:

(7)组与非捕获组

以下提供一些简单的示例:

 string x = "live for nothing,die for something";
 string y = "live for nothing,die for somebody";
 regex r = new regex(@"^live ([a-z]{}) no([a-z]{}),die / some/$");
 console.writeline("x match count:" + r.matches(x).count);//
 console.writeline("y match count:" + r.matches(y).count);//
 //正则表达式引擎会记忆“()”中匹配到的内容,作为一个“组”,并且可以通过索引的方式进行引用

表达式中的“/”,用于反向引用表达式中出现的第一个组,即粗体标识的第一个括号内容,“/”则依此类推。

 string x = "live for nothing,die for something";
 regex r = new regex(@"^live for no([a-z]{}),die for some/$");
 if (r.ismatch(x))
 {
 console.writeline("group value:" + r.match(x).groups[].value);//输出:thing
 }
 //获取组中的内容。注意,此处是groups[],因为groups[]是整个匹配的字符串,即整个变量x的内容。
 // http://www.cnblogs.com/sosoft/
 
 string x = "live for nothing,die for something";
 regex r = new regex(@"^live for no(?<g>[a-z]{}),die for some/$");
 if (r.ismatch(x))
 {
 console.writeline("group value:" + r.match(x).groups["g"].value);//输出:thing
 }
 //可根据组名进行索引。使用以下格式为标识一个组的名称(?<groupname>…)。
 string x = "live for nothing nothing";
 regex r = new regex(@"([a-z]+) /");
 if (r.ismatch(x))
 {
 x = r.replace(x, "$");
 console.writeline("var x:" + x);//输出:live for nothing
 }
 //删除原字符串中重复出现的“nothing”。在表达式之外,使用“$”来引用第一个组,下面则是通过组名来引用:
 string x = "live for nothing nothing";
 regex r = new regex(@"(?<g>[a-z]+) /");
 if (r.ismatch(x))
 {
 x = r.replace(x, "${g}");
 console.writeline("var x:" + x);//输出:live for nothing
 }
 string x = "live for nothing";
 regex r = new regex(@"^live for no(?:[a-z]{})$");
 if (r.ismatch(x))
 {
 console.writeline("group value:" + r.match(x).groups[].value);//输出:(空)
 }
//在组前加上“?:”表示这是个“非捕获组”,即引擎将不保存该组的内容

(8)贪婪与非贪婪

正则表达式的引擎是贪婪,只要模式允许,它将匹配尽可能多的字符。通过在“重复描述字符”(*,+)后面添加“?”,可以将匹配模式改成非贪婪。请看以下示例:

 string x = "live for nothing,die for something";
 regex r = new regex(@".*thing");
 if (r.ismatch(x))
 {
 console.writeline("match:" + r.match(x).value);//输出:live for nothing,die for something
 }
 regex r = new regex(@".*?thing");
 if (r.ismatch(x))
 {
 console.writeline("match:" + r.match(x).value);//输出:live for nothing
 }

(9)回溯与非回溯

使用“(?>…)”方式进行非回溯声明。由于正则表达式引擎的贪婪特性,导致它在某些情况下,将进行回溯以获得匹配,请看下面的示例:

string x = "live for nothing,die for something";
 regex r = new regex(@".*thing,");
 if (r.ismatch(x))
 {
 console.writeline("match:" + r.match(x).value);//输出:live for nothing,
 }
 regex r = new regex(@"(?>.*)thing,");
 if (r.ismatch(x))//不匹配
 {
 console.writeline("match:" + r.match(x).value);
 }
 //在r中,“.*”由于其贪婪特性,将一直匹配到字符串的最后,随后匹配“thing”,但在匹配“,”时失败,此时引擎将回溯,并在“thing,”处匹配成功

 //在r中,由于强制非回溯,所以整个表达式匹配失败。

(10)正向预搜索、反向预搜索

正向预搜索声明格式:正声明 “(?=…)”,负声明 “(?!...)” ,声明本身不作为最终匹配结果的一部分,请看下面的示例:

 string x = " used free";
 regex r = new regex(@"/d{}(?= used)");
 if (r.matches(x).count==)
 {
 console.writeline("r match:" + r.match(x).value);//输出:
 }
 regex r = new regex(@"/d{}(?! used)");
 if (r.matches(x).count==)
 {
 console.writeline("r match:" + r.match(x).value); //输出:
 }
 //r中的正声明表示必须保证在四位数字的后面必须紧跟着“ used”,r2中的负声明表示四位数字之后不能跟有“ used”

反向预搜索声明格式:正声明“(?<=)”,负声明“(?<!)”,声明本身不作为最终匹配结果的一部分,请看下面的示例:

string x = "used: free:";
 regex r = new regex(@"(?<=used:)/d{}");
 if (r.matches(x).count==)
 {
 console.writeline("r match:" + r.match(x).value);//输出:
 }
 regex r = new regex(@"(?<!used:)/d{}");
 if (r.matches(x).count==)
 {
 console.writeline("r match:" + r.match(x).value);//输出:
 }
 //r中的反向正声明表示在4位数字之前必须紧跟着“used:”,r2中的反向负声明表示在4位数字之前必须紧跟着除“used:”之外的字符串

(11)十六进制字符范围

正则表达式中,可以使用 "/xxx" 和 "/uxxxx" 表示一个字符("x" 表示一个十六进制数)形式字符范围:
/xxx       编号在 0到255 范围的字符,比如:空格可以使用 "/x20" 表示。
/uxxxx   任何字符可以使用 "/u" 再加上其编号的4位十六进制数表示,比如:汉字可以使用“[/u4e00-/u9fa5]”表示。

(12)对[0,100]的比较完备的匹配

下面是一个比较综合的示例,对于匹配[0,100],需要特殊考虑的地方包括
*00合法,00.合法,00.00合法,001.100合法
*空字符串不合法,仅小数点不合法,大于100不合法
*数值是可带后缀的,如“1.07f”表示该值为一个float类型(未考虑)

 regex r = new regex(@"^/+?*(?:(/.*)?|(/d{,}(?=/./d)|/d{,}(?=($|/.$)))(/./d*)?)$");
 string x = "";
 while (true)
 {
 x = console.readline();
 if (x != "exit")
 {
  if (r.ismatch(x))
  {
  console.writeline(x + " succeed!");
  }
  else
  {
  console.writeline(x + " failed!");
  }
 }
 else
 {
  break;
 }
 }

(13)精确匹配有时候是困难的

有些需求要做到精确匹配比较困难,例如:日期、url、email地址等,其中一些你甚至需要研究一些专门的文档写出精确完备的表达式,对于这种情况,只能退而求其次,保证比较精确的匹配。例如对于日期,可以基于应用系统的实际情况考虑一段较短的时间,或者对于像email的匹配,可以只考虑最常见的形式。

以上内容是小编给大家介绍的c#正则表达式,希望大家喜欢。同时感谢大家一直以来对移动技术网网站的支持,祝大家元旦快乐。

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

相关文章:

验证码:
移动技术网