string x="D://My Huang//My Doc"; string y = @"D:/My Huang/My Doc";
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
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通常用于约束一个完整的单词
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/
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);//
//匹配到的数
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);//输出:(空)
}
//在组前加上“?:”表示这是个“非捕获组”,即引擎将不保存该组的内容
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
}
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,”处匹配成功
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:”之外的字符串
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;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有