常用脚本(十一)C#_正则表达式

使用单个字符串来描述,匹配一系列符合某个语法规则的字符串,通常会被用来检索与替换某些文本。

 

原义文本字符:正常的文本。

元字符:具有一些特殊意义的字符,是用来代替正常文本的字符。 

限定符:限定匹配的数量。 

元字符:

                    匹配除了换行以外的任意字符

\w                  匹配字母、数字、下划线、汉字

\s                   匹配任意空白符

\d                   匹配任意的数字

[7-9]               匹配7到9

^                    匹配字符串的开头

$                    匹配字符串的结尾

 

限定符

{n}                 重复n次

{n,}                重复大于等于n次

{n,m}             重复n到m次

*                    重复大于等于0次,相当于{0,}

+                   重复大于等于1次,相当于{1,}

?                 重复0次或1次,相当于{0,1}

 

比如说:

要求字母开头,5-16位,字母数字下划线

^[a-zA-Z][a-zA-Z0-9_]{4-15}$

 

代码中:

注意引入命名空间:using System.Text.RegularExpressions; 

1.判断input符合不符合规则

2.匹配,如果满足,打印出满足条件的任容

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

namespace Memory_2
{
    class Program
    {
        static void Main(string[] agrs)
        {
            string input = "jnpgc123";
            string pattern = @"^[a-zA-Z][a-zA-Z0-9_]{4,15}$";
            Regex regex = new Regex(pattern);
            //1.判断input符合不符合规则
            bool res = regex.IsMatch(input);
            Console.WriteLine(res);
            //2.匹配,如果满足,打印出满足条件的任容
            Match match = regex.Match(input);
            Console.WriteLine(match.Value);

            Console.ReadKey();
        }
    }
}



----->Ture
----->jnpgc123

3.匹配所有满足条件的内容

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

namespace Memory_2
{
    class Program
    {
        static void Main(string[] agrs)
        {
            string input = "jnpgc123saDASD000SDF";
            string pattern = @"\d\d\d"; //匹配三个任意数字
            Regex regex = new Regex(pattern);
            //匹配所有满足条件的内容
            MatchCollection mc = regex.Matches(input);
            foreach (Match m in mc)
            {
                Console.WriteLine(m.Value);
            }

            Console.ReadKey();
        }
    }
}



----->123
----->000

4.正则表达式特性:贪婪模式

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

namespace Memory_2
{
    class Program
    {
        static void Main(string[] agrs)
        {
            string input = "<a><b>";
            string pattern = @"<.*>";
            Regex regex = new Regex(pattern);
            //匹配所有满足条件的内容
            MatchCollection mc = regex.Matches(input);
            foreach (Match m in mc)
            {
                Console.WriteLine(m.Value);
            }

            Console.ReadKey();
        }
    }
}




-----><a><b>

本来以为会输出<a>,结果<a><b>都输出出来了,这个就是正则表达式的特性——贪婪模式

那么,如何取消贪婪模式呢?

将  string pattern = @"<.*>";  改为  string pattern = @"<.*?>";

就可以了,运行一下会输出<a>。

在这里‘?’表示另外一个意思——取消贪婪模式(原本的意思是重复0次或1次,相当于{0,1})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值