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

来源:互联网 发布:java用户角色权限设计 编辑:程序博客网 时间:2024/05/21 12:48

这里介绍以下正则表达式的基本用法,.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();
        }
    }
}

原创粉丝点击