正则表达式适合很多种语言不单单是c#,
. 匹配除换行符以外的任意字符
w 匹配字母,数字,下划线,汉字
W 小w的补集 就是和小w相反的
s 匹配 任意空白符(包括换行符/n,回车符/r,制表符/t,垂直制表符/v,换页符/f )
d 匹配数字(0-9数字)
[abc] 匹配括号中的字符
[a-c] a字符到c字符
[^x] 匹配意外的任意字符
[^adwz] 匹配除了adwz以外的字符
{n}匹配前面的字符n次
{n.} 匹配前面的字符n次或对于n次
{n,m} 匹配前面的字符n到m次
? 重复零次或者一次
+ 再重复一次或者更多
* 重复零次 或者更多
??
using System.Text.RegularExpressions; 这个命名空间要加上
namespace string字符串 { class Program { static void Main(string[] args) { string s = "www.devsiki.com"; int length = s.Length; //字符串的长度 //if (s == "www.devsiki.com") { //字符串可以直接比较 // Console.Write("相同"); //} //else //{ // Console.Write("不相同"); //} // s="http://"+s; //字符串的加法 // Console.Write(s); // char c = s[3]; //取得单个字母 // for (int i = 0; i < s.Length; i++) //游历每个字符 // { // Console.WriteLine(s[i]); // } // string a = "I am big one."; // string res =Regex.Replace(s,"^","开始:");//在头部添加
// string abc = Regex.Replace(s,"$","结束");//在尾部添加 I am big one.结束
//string shuru= Console.ReadLine(); //任意输入 // string pattern = @"^d*$"; //检测是否为存数字 // bool isMatch = Regex.IsMatch(shuru,pattern); // Console.WriteLine(isMatch);
// //ahou意外的字符用*代替 // string str = "I am surpman"; // string pattern1 = @"[^ahou]"; // string s1 = Regex.Replace(str,pattern1,"*"); // //重复位 数字重复5到12次就是5到12位,重开头到结尾是不是5到12位的数字 // string qq1 = "234234"; // string pattern2 = @"^d{5,12}$"; // Console.WriteLine(Regex.IsMatch(qq1,pattern2)); // //把a到z的字母检索出来 // string s2 = "34((*&sdfil路口设计"; // string pattern3 = @"d[a-z]"; // MatchCollection col = Regex.Matches(s,pattern3); // foreach (Match macth in col) // { // Console.WriteLine(macth); // }
//分割 //string s1 = "zhangan;lisi,wangyu.zhaoli"; ////string pattern1 = @"[;,.]"; //string pattern1 = @"[;][,][.]"; //string[] resArray = Regex.Split(s,pattern1); //foreach (var s2 in resArray) //{ // Console.WriteLine(s1); //} //重复 ab开头w重复2次 然后 整组重复2次相当于abwwabww string strGroup2 = @"(abw{2}){2}"; Console.ReadKey();
} } }
??
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|