这里介绍以下正则表达式的基本用法,.net库提供了一个很简单的类给我们使用
这个类是 Regex,很方便。
下面是基本的用法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace 正则表达式1
{
class Program
{
static void Main(string[] args)
{
Regex reg = new Regex("the");
string str = "the pig is very foolish!!";
Match matchSet = reg.Match(str);
if (matchSet.Success)
{
Console.WriteLine("the 是在句子中的第{0}个单词", matchSet.Index); //序号从0 开始
}
else
{
Console.WriteLine("找不到the");
}
Console.Read();
}
}
}