C#:正则表达式类

 Regex r = new Regex("abc"); // 定义一个Regex对象实例
(Regex r = new Regex("abc", RegexOptions.IgnoreCase);//不区分大小写的正则)
 
Match m = r.Match("123abc456"); // 在字符串中匹配
 
if (m.Success)
 
{
 
Console.WriteLine("Found match at position " + m.Index); //输入匹配字符的位置
 
}
// Match类


MatchCollection mc;
 
String[] results = new String[20];
 
int[] matchposition = new int[20];
 
Regex r = new Regex("abc"); //定义一个Regex对象实例
 
mc = r.Matches("123abc4abcd");
 
for (int i = 0; i < mc.Count; i++) //在输入字符串中找到所有匹配
 
{
 
results[i] = mc[i].Value; //将匹配的字符串添在字符串数组中
 
matchposition[i] = mc[i].Index; //记录匹配字符的位置
 
} //MatchCollection类


string Str = "AppleOrOrange"
Str = Regex.Replace(Str, "or", "", RegexOptions.IgnoreCase)
//直接将字符串内的Or、oR、or、OR替换成空格
posted @ 2018-03-06 17:24  程序猿kid  阅读(345)  评论(0编辑  收藏  举报