您的位置:首页 > 编程语言 > C#

正则表达式的基本用法(C#)

2012-07-01 16:14 288 查看
这里介绍以下正则表达式的基本用法,.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();

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: