c# 正则表达式

--正则表达式
1.简单语法之间匹配规则
字符语法                        语法
\d         ---------------------匹配数字(0~9)
\D         ---------------------匹配非数字
\w         ---------------------匹配字母或数字或下划线或汉字
\W         ---------------------匹配非单词
\s         ---------------------匹配空白字符
\S         ---------------------匹配非空字符
.          ---------------------匹配任意字符
[...]      ---------------------匹配括号中的任意字符
^          ---------------------匹配字符串中的开始
$          ---------------------匹配字符串的结尾
\b         ---------------------匹配一个单词边界
\B         ---------------------匹配一个单词非边界部分
/.         ---------------------查找位置
^\d{3}$    ---------------------从开始到结束查找3位数字
2.简单语法之间匹配规则
字符语法                         说明、
{n}        ----------------------匹配N次字符
{n,}       ----------------------匹配N次和N次以上字符
{n,m}      ----------------------匹配N次以上M次以下字符
?          ----------------------匹配0或1次
+          ----------------------匹配1次或多次
*          ----------------------匹配0次以上
[^...]     ----------------------匹配非括号字符
[^0-9]     ----------------------匹配0-9字符


1{2}       ----------------------匹配1限定符出现2次
^\d{2,}$   ----------------------匹配整数2位以上数字


17.5正则表达式替换
.要通过正则表达式替换字符串中的匹配项,就要通过Regex类的Replace方法
有多种格式,
.格式
.Regex.Replace(要搜索匹配项的字符串,要替换的原字符串,替换后的字符串)


17.6正则表达式拆分
.要通过正则表达式拆分字符串,就要通过Regex类的Split方法,格式为:

.Regex.Split(要拆分的字符串,要匹配的正则表达式模式);


1.Replace

using System;

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


namespace _17._5_Replace
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = "Welcome to www.51zxw.net";
            string pattern = @"\bw{3}\.\w+\.(com|net|org)\b";
            string replacement = @"http:\\$&";
            Console.WriteLine("替换前的字符串:"+Regex.Replace(input,pattern,replacement,RegexOptions.IgnoreCase));//RegexOptions忽略大小写
            Console.WriteLine(Regex.Match(input,pattern));




            Regex myregex = new Regex(pattern, RegexOptions.IgnoreCase);//RegexOptions忽略大小写
            string result = myregex.Replace(input, replacement);
            Console.WriteLine("替换后的字符串3:" + result);
            Console.ReadKey();
        }
    }

}


2.Split

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


namespace _17._6_Split
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = "一、张三 二、李四 三、王五 四、赵六 五、何七";
            string pattern = @"\b[一二三四五]、";
            foreach(string outstr in Regex.Split(input,pattern))
            {
                if (!string.IsNullOrEmpty(outstr)) Console.WriteLine(outstr);
            }
            Console.ReadKey();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值