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

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

2017年12月12日  | 移动技术网IT编程  | 我要评论

(4)重复描述字符
“重复描述字符”是体现c#正则表达式“很好很强大”的地方之一:
{n} 匹配前面的字符n次
{n,} 匹配前面的字符n次或多于n次
{n,m} 匹配前面的字符n到m次
? 匹配前面的字符0或1次
+ 匹配前面的字符1次或多于1次
* 匹配前面的字符0次或式于0次
以下提供一些简单的示例:
复制代码 代码如下:

string x = "1024";
string y = "+1024";
string z = "1,024";
string a = "1";
string b="-1024";
string c = "10000";
regex r = new regex(@"^\+?[1-9],?\d{3}$");
console.writeline("x match count:" + r.matches(x).count);//1
console.writeline("y match count:" + r.matches(y).count);//1
console.writeline("z match count:" + r.matches(z).count);//1
console.writeline("a match count:" + r.matches(a).count);//0
console.writeline("b match count:" + r.matches(b).count);//0
console.writeline("c match count:" + r.matches(c).count);//0
//匹配1000到9999的整数。

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

string x = "0";
string y = "0.23";
string z = "100";
string a = "100.01";
string b = "9.9";
string c = "99.9";
string d = "99.";
string e = "00.1";
regex r = new regex(@"^\+?((100(.0+)*)|([1-9]?[0-9])(\.\d+)*)$");
console.writeline("x match count:" + r.matches(x).count);//1
console.writeline("y match count:" + r.matches(y).count);//1
console.writeline("z match count:" + r.matches(z).count);//1
console.writeline("a match count:" + r.matches(a).count);//0
console.writeline("b match count:" + r.matches(b).count);//1
console.writeline("c match count:" + r.matches(c).count);//1
console.writeline("d match count:" + r.matches(d).count);//0
console.writeline("e match count:" + r.matches(e).count);//0
//匹配0到100的数。最外层的括号内包含两部分“(100(.0+)*)”,“([1-9]?[0-9])(\.\d+)*”,这两部分是“or”的关系,即正则表达式引擎会先尝试匹配100,如果失败,则尝试匹配后一个表达式(表示[0,100)范围中的数字)。

(6)特殊字符的匹配
下面提供一些简单的示例:

view plaincopy to clipboardprint?
string x = "\\";
regex r1 = new regex("^\\\\$");
console.writeline("r1 match count:" + r1.matches(x).count);//1
regex r2 = new regex(@"^\\$");
console.writeline("r2 match count:" + r2.matches(x).count);//1
regex r3 = new regex("^\\$");
console.writeline("r3 match count:" + r3.matches(x).count);//0
//匹配“\”

string x = "\"";
regex r1 = new regex("^\"$");
console.writeline("r1 match count:" + r1.matches(x).count);//1
regex r2 = new regex(@"^""$");
console.writeline("r2 match count:" + r2.matches(x).count);//1
//匹配双引号
string x = "\\";
regex r1 = new regex("^\\\\$");
console.writeline("r1 match count:" + r1.matches(x).count);//1
regex r2 = new regex(@"^\\$");
console.writeline("r2 match count:" + r2.matches(x).count);//1
regex r3 = new regex("^\\$");
console.writeline("r3 match count:" + r3.matches(x).count);//0
//匹配“\”

string x = "\"";
regex r1 = new regex("^\"$");
console.writeline("r1 match count:" + r1.matches(x).count);//1
regex r2 = new regex(@"^""$");
console.writeline("r2 match count:" + r2.matches(x).count);//1
//匹配双引号



(7)组与非捕获组
以下提供一些简单的示例:
复制代码 代码如下:

string x = "live for nothing,die for something";
string y = "live for nothing,die for somebody";
regex r = new regex(@"^live ([a-z]{3}) no([a-z]{5}),die \1 some\2$");
console.writeline("x match count:" + r.matches(x).count);//1
console.writeline("y match count:" + r.matches(y).count);//0
//正则表达式引擎会记忆“()”中匹配到的内容,作为一个“组”,并且可以通过索引的方式进行引用。表达式中的“\1”,用于反向引用表达式中出现的第一个组,即粗体标识的第一个括号内容,“\2”则依此类推。

string x = "live for nothing,die for something";
regex r = new regex(@"^live for no([a-z]{5}),die for some\1$");
if (r.ismatch(x))
{
console.writeline("group1 value:" + r.match(x).groups[1].value);//输出:thing
}
//获取组中的内容。注意,此处是groups[1],因为groups[0]是整个匹配的字符串,即整个变量x的内容。

string x = "live for nothing,die for something";
regex r = new regex(@"^live for no(?<g1>[a-z]{5}),die for some\1$");
if (r.ismatch(x))
{
console.writeline("group1 value:" + r.match(x).groups["g1"].value);//输出:thing
}
//可根据组名进行索引。使用以下格式为标识一个组的名称(?<groupname>…)。

string x = "live for nothing nothing";
regex r = new regex(@"([a-z]+) \1");
if (r.ismatch(x))
{
x = r.replace(x, "$1");
console.writeline("var x:" + x);//输出:live for nothing
}
//删除原字符串中重复出现的“nothing”。在表达式之外,使用“$1”来引用第一个组,下面则是通过组名来引用:
string x = "live for nothing nothing";
regex r = new regex(@"(?<g1>[a-z]+) \1");
if (r.ismatch(x))
{
x = r.replace(x, "${g1}");
console.writeline("var x:" + x);//输出:live for nothing
}

string x = "live for nothing";
regex r = new regex(@"^live for no(?:[a-z]{5})$");
if (r.ismatch(x))
{
console.writeline("group1 value:" + r.match(x).groups[1].value);//输出:(空)
}
//在组前加上“?:”表示这是个“非捕获组”,即引擎将不保存该组的内容。

2

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

相关文章:

验证码:
移动技术网