C#正则表达式

正则表达式使用一种数学算法来解决计算机程序中的文本检索,匹配等问题,正则表达式语言是一种专门用来处理字符串的语言。可以认为正则表达式表达了一个字符串的书写规则。

正则表达式是由普通字符以及特殊字符(元字符)组成的文字模式

下面上几个例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Text.RegularExpressions;
namespace Unity
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "I am blue cat.";
            string res1 = Regex.Replace(s, "^", "开始:");//搜索字符串,符合正则表达式的情况。然后把所以符合的位置替换成后面的字符串"^"(定位开始正则表达式)
            string res2 = Regex.Replace(s, "$", "开始:");//搜索字符串,符合正则表达式的情况。然后把所以符合的位置替换成后面的字符串"^"(定位结尾正则表达式)
            Console.WriteLine(res1);
            Console.WriteLine(res2);
         }
     }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Text.RegularExpressions;
namespace Unity
{
    class Program
    {
        static void Main(string[] args)
        {
             string s = Console.ReadLine();
            string pattern = @"^\d*$";//正则表达式(全部由数字组成),\d代表数字
            bool isMatch = Regex.IsMatch(s,pattern);//判读是否符合正则表达式
         }
     }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Text.RegularExpressions;
namespace Unity
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = Console.ReadLine();
            string pattern = @"^\w*$";//匹配包括下划线的任何单词字符,\w代表包括下划线的任何单词字符
            bool isMatch = Regex.IsMatch(s,pattern);

         }
     }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Text.RegularExpressions;
namespace Unity
{
    class Program
    {
        static void Main(string[] args)
        {
           string str = "I am a cat";
            string pattern = @"[^ahou]";//匹配除了括号里的其它字符,例如这个例子结果为**a**a**a*
            string s = Regex.Replace(str, pattern, "*");
            Console.WriteLine(s);

         }
     }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Text.RegularExpressions;
namespace Unity
{
    class Program
    {
        static void Main(string[] args)
        {
           string qq1 = "231564";
            string qq2 = "121321245654879";
            string qq3 = "d1231456";
            string pattern = @"^\d{5,12}$";//{n,m}重复字符的次数n到m之间,这个正则表达式可以用来匹配5到12位的数字
            Console.WriteLine(Regex.IsMatch(qq1,pattern));
            Console.WriteLine(Regex.IsMatch(qq2, pattern));
            Console.WriteLine(Regex.IsMatch(qq3, pattern));

         }
     }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值