C#中的正则 Regex类

时间:2020-12-26 16:39:46

转义符的总结:

开始数

………………………………………………………….

3 )Regex.matches() 提取所有符合要求的字符串

string str=@"绩大于80,则奖励50元.语文成绩等于100并且音乐成绩大于70,则奖励100元";

MatchCollection matches = Regex.Matches(str, "[0-9]+"); //用 MatchCollection 类型的变量存储匹配到的字符

foreach (Match mc in matches) //遍历输出得到的字符串

{

Console.WriteLine(mc.Value);

}

Console.ReadKey();

………………………………………………………….

4)Regex.Replace 替换字符串     有两种替换方式

方式1:

str = Regex.Replace(str, "aa+", "");

方式2:

string bir = "我的生日是05/21/2010耶";

string result = Regex.Replace(bir,@"(\d{2})/(\d{2})/(\d{4})","$3-$1-$2");

………………………………………………………….

5)Regex.Split 分割:

string str1="jfgdjgkhgkeydijf;ldfjgldfkey";

string[] strArr = Regex.Split(str1, "key.");

for (int i = 0; i < strArr.Length; i++)

{

Console.WriteLine(strArr[i]);

}

Console.ReadKey();