chapter18正则表达式:学习“或”元字符

|        将两个匹配条件进行逻辑“或”运算

()       用小括号指定子表达式(也就是分组)

例如:重复多个(abcd){n}进行分组限定。

string str = "同学们同学们”;

string strPattern = @"(同学们){2}";

Regex.IsMatch(str, strPattern);

源代码:

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


namespace chapter18学习或运算与小括号元字符
{
    class Program
    {
        /// <summary>
        /// 在文本中,检索字符或数字
        /// </summary>
        public void Test1()
        {
            string str = "2467889jckclslsll*&^%ghj876";
            string strPattern = @"\d|[a-z]";
            MatchCollection col = Regex.Matches(str, strPattern);
            foreach (Match item in col)
            {
                Console.WriteLine(item);
            }
        }
        /// <summary>
        /// 提取博客的标签
        /// </summary>
        public void Test2()
        {
            string[] strTagArray;  //标签数组
            string strTag = "C#,C#教程.C#讲义;C#书籍";
            string strPattern = @"[,]|[.]|[;]";
            strTagArray = Regex.Split(strTag, strPattern);
            //显示结果
            foreach (var item in strTagArray)
            {
                Console.WriteLine(item);
            }
        }
        static void Main(string[] args)
        {
            Program obj = new Program();
            //obj.Test1();
            obj.Test2();
            Console.ReadKey();
        }
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值