C# 正则表达式

定位点:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            string things = "294 623_7857@qq.com
dddd";
            string pattern1 = @"^d{3}";  // ^:以什么在字符串或者行开头,输出“294”
            //
            string pattern2 = @"om$";  // $:以什么在字符串或者行结尾,输出“om”
            //
            string pattern3 = @"Ad{3}";  // A:匹配出现在字符串的开头,输出“294”
            //
            string pattern4 = @"com";  // :匹配出现在字符串的末尾或者
之前的字符,输出“com”。z:匹配必须出现在末尾的字符
            //
            string pattern5 = @"com";  // :匹配边界字符。B:匹配不得出现在边界上的字符
            MatchCollection mc = Regex.Matches(things, pattern5);
            foreach(Match item in mc)
            {
                Console.WriteLine(item);
            }
        }
    }
}

分组构造: 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            /*
            string things = "7ddb 7dbb 7dcbc 7ddbb";
            // (?<=name)value:先判断name与最左侧部分是否匹配,如果匹配了在匹配value是否等于右半边部分
            string pattern1 = @"(?<=7)[a-z]{3}";  // 本例子中,匹配开头等于7的,后面加上3个字符和一个边界的字符串
            MatchCollection mc = Regex.Matches(things, pattern1);
            */
            //
            /*
            string things1 = "myname namejr myage 12 mysex man";
            // (?!name)value:匹配开始不为name和结尾为value的字符
            string pattern2 = @"(?!my)w{2,6}";  // 这里为以边界为划分,匹配开头不等于"my"且结尾为2到6个任意单词字符 的字符
            MatchCollection mc = Regex.Matches(things1, pattern2);
            */
            /*
            string things1 = "myname yourname hisname";
            // (?=name):判断最右半边的部分是否等于name,在比较左半边
            string pattern1 = @"w+(?=name)";  // 这里是获取最后一部分是name,前面是任意单词的字符
            MatchCollection mc = Regex.Matches(things1, pattern1);
            */
            //
            /*
            string things1 = "watting looking talkingaskingaa";
            // 全部匹配输出
            string pattern1 = @"w{3}(?:ing)";  // 获取有关"ing"以及往前三个任意单词的字符
            MatchCollection mc = Regex.Matches(things1, pattern1);
            */
            string things = "1ABB 3ABBC 5AB 5AC";
            string pattern1 = @"(?<=d)[A-C]{2,4}";  // 这个表达式匹配出来的是:ABB ABBC AB AC
            string pattern2 = @"(?:d)[A-C]{2,4}";  // 这个表达式匹配出来的是:1ABB 3ABBC 5AB 5AC
            string pattern3 = @"d(?=[A-C]{2,4})";  // 这个表达式匹配出来的是:1 3 5 5
            MatchCollection mc = Regex.Matches(things, pattern3);
            foreach (Match item in mc)
            {
                Console.WriteLine(item);
            }
        }
    }
}

 限定符:

*:匹配上一个元素的零次或者多次。
+:匹配上一个元素的一次或者多次。
?:匹配上一个元素的零次或者一次。
{n,}:匹配上一个元素至少n次。
{n,m}:匹配上一个元素最少n次且最多m次。
*?:匹配上一个元素零次或者多次,但是尽可能少的匹配(懒惰模式)。
+?:匹配上一个元素一次或者多次,但是尽可能少的匹配(懒惰模式)。
??:匹配上一个元素零次或者一次,但是尽可能少的匹配(懒惰模式)。
{n}?:匹配前导元素恰好n次。
{n,}?:匹配上一个元素至少n次,但是尽可能少的匹配(懒惰模式)。
{n,m}?:匹配上一个元素至少n次并且最多m次,而且尽可能少的匹配(懒惰模式)。

 反向引用构造:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            // 什么是反向引用构造?
            // 个人理解就是反向引用构造可以使用一个小括号()来获取匹配的值,后面可以按照小括号的排序来进行获取小括号里面相同的值
            string myStr = "456abc456";
            string pattern1 = @"(d+)abc1";  // 这里小括号获取的就是三个数字,而"1"表示第一个小括号。这里小括号取的值为"456",所以"1"的值也为"456"
            MatchCollection mc = Regex.Matches(myStr, pattern1);
            foreach(Match item in mc)
            {
                Console.WriteLine(item);
            }
            Console.Clear();
            //
            // 除了上面这个反向引用外,我们还可以进行对其命名
            // (?<name>)k<name>:可以使用"k<name>"来代替前面的(?<name>)
            string myStr1 = "ABD874asdf41a5sd4fa5sABDd74f";
            string pattern2 = @"(?<name>[A-D]+)[a-z1-9]+k<name>[a-z1-9]+";
            MatchCollection mc1 = Regex.Matches(myStr1, pattern2);
            foreach(Match item in mc1)
            {
                Console.WriteLine(item);
            }
        }
    }
}

备用构造:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            // (?(expression))yes|no:如果正则表达式模式由expression指定匹配则匹配yes,否者匹配no
            string myStr1 = "myname,yourname,hisname";
            string pattern1 = @"(?(name))[a-z]{2,3}name|[a-z]{4}name";
            MatchCollection mc = Regex.Matches(myStr1, pattern1);
            foreach(Match item in mc)
            {
                Console.WriteLine(item);
            }
        }
    }
}

替换:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            // 替换
            string myStr1 = "jr name";
            // $n:可以用来表示第n个小括号的值
            string r = Regex.Replace(myStr1, @"(w+)(s)(w+)", @"$3$2$1");
            Console.WriteLine(r);
            Console.Clear();
            //
            // ${name}:表示按照命名参数来划分
            string myStr2 = "my name is jr";
            string r2 = Regex.Replace(myStr2, @"(?<name1>w+)s(?<name2>w+)s(?<name3>w+)s(?<name4>w+)", @"${name1} ${name3} ${name2}${name4}");
            Console.WriteLine(r2);
        }
    }
}

 查找:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            string MyStr = @"ndo.121/,,a撒d.d旦21.,.dd搜房,..45那65dd是的74asd那份·1";
            // Regex.Match()
            Match mc = Regex.Match(MyStr, @"d+");  // 使用功能Regex.Match()匹配一个合适的字符
            Console.WriteLine(mc);  // 输出"121"
            Console.Clear();
            //
            // Regex.Matches()
            MatchCollection mcs = Regex.Matches(MyStr, @"d+");  // 使用功能Regex.Matches()匹配所有合适的字符
            foreach(Match item in mcs)
            {
                Console.Write(item+" ");  // 输出"121 21 45 65 74 1 "
            }
        }
    }
}
原文地址:https://www.cnblogs.com/namejr/p/10452967.html