正则表达式(1)

很早就看了正则表达式,python啊,c++什么的都有,从来没细致学过,最近突然看到C#更强大,而且居然找到了一个《C#字符串和正则表达式参考手册.pdf》,就随便记些什么吧。程序员的老规矩,上代码,跑一遍比什么理论都强。

1、IsMatch()方法:最简单的一个匹配程序

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Text.RegularExpressions;
namespace regex1 
{ 
    class Program 
    { 
        static void Main(string[] args) 
        { 
            Regex myRegEx = new Regex("ABC", RegexOptions.IgnoreCase); 
            Console.WriteLine(myRegEx.IsMatch("The first three letters of" + "the alphabet ar ABC")); 
        } 
    } 
} 

需要注意的一点,默认情况下,Regex类匹配的是ASCII文本。

IsMatch有静态重载函数,所以我们可以省掉Regex类的申明,以下为简单实例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
 
namespace regex1
{
    class Program
    {
        static void Main(string[] args)
        {
            string inputString = "Welcome to the publishers, Wrox Press Ltd";
            if (Regex.IsMatch(inputString, "wrox press", RegexOptions.IgnoreCase))
            {
                Console.WriteLine("Match Found");
            }
            else
            {
                Console.WriteLine("No Match found");
            }
        }
    }
}

我们发现,加入IgnoreCase后,便能够对大小写不再敏感。

2、Replace()方法:

发现匹配的目的更多的其实是为了替换成其它的某类字符串,使用Replace即可,重载很多,只列出一些基本用法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
 
namespace regex1
{
    class Program
    {
        static void Main(string[] args)
        {
            string inputString = "Welcome to the publishers wrox press ltd";
            inputString = Regex.Replace(inputString, "wrox press", "Wrox Press");
            Console.WriteLine(inputString);
        }
    }
}

非静态方法有更多可控制的,比如替换的次数及从第几个开始:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
 
namespace regex1
{
    class Program
    {
        static void Main(string[] args)
        {
            string inputString = "123,456,123,123,789,123,888";
            Regex regExp = new Regex("123");
            inputString = regExp.Replace(inputString, "xxx", 2, 4);
            Console.WriteLine(inputString);
        }
    }
}

生成结果如下:123,456,xxx,xxx,789,123,888

参数2表示替换总数为2次,4表示从下标4开始(下标从0开始表示),所以第1个123不会被替,最后一个也不会被替

3、Split()方法:

此方法在每次发现匹配的位置拆分字符串,返回一个字符串数组:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace regex1
{
    class Split
    {
        static void Main(string[] args)
        {
            string inputString = "123,ABC,456,DEF,789";
            string[] splitResults;
            splitResults = Regex.Split(inputString, ",");
            foreach(string str in splitResults)
                Console.WriteLine(str);
        }
    }
}

4、Match类和MatchCollection类

前面或者是计算出匹配几次或者是直接替换匹配的文本,Match则是告诉我们匹配的细节。以下代码,打印出匹配的所有文本:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
 
namespace regex1
{
    class MatchMethod
    {
        static void Main(string[] args)
        {
            string inputString = "A sailor went to sea to sea," 
                + "to see what he could see could see.";
            Regex myRegex = new Regex("se.");
            Match matchMade = myRegex.Match(inputString, 20);
 
            while (matchMade.Success)
            {
                Console.WriteLine(matchMade.Value);
                matchMade = matchMade.NextMatch();
            }
        }
    }
}

注意:"se.",此为简单的正则表达式, 表示匹配3个字符,前两个为se,最后一位为任意字符,即匹配任意se打头的串

5、附一个简单的正则表达式解析器——这儿没法上传,先传到csdn资源区,没有分的朋友可以直接向我要:http://download.csdn.net/detail/luhouxiang/7223749

原文地址:https://www.cnblogs.com/luhouxiang/p/3675954.html