当前位置:编程学习 > C#/ASP.NET >>

C#之正则表达式使用实例讲解

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

namespace 字符串
{
    class 正则表达式
    {
        public static void Main()
        {
            //正则表达式是用来简化字符串操作的,一般正则所实现的功能,用方法也一样可以,只是方法有可能就会变的很复杂!
            //假定我们要在字符串中做一次纯文件的搜索,不用任何的正则串。
            string Text = @"This comprehensive compendium provides a boroad and thor
                            This comprehensive compendium provides a boroad and thor
                            This comprehensive compendium provides a boroad and thor
                            This comprehensive compendium provides a boroad and thor
                            This comprehensive compendium provides a boroad and thor";
            string Pattern = "thor";

            MatchCollection Matchs = Regex.Matches( Text , Pattern , RegexOptions.IgnoreCase );

            foreach (Match i in Matchs)
            {
                Console.WriteLine( i.Value );
            }
            
            //例:我们要查看我们的单词是不是以N开头
            string myText = "Naladdin";
            Match mat = Regex.Match( myText , @"n" , RegexOptions.IgnoreCase );
            Console.WriteLine(myText "是否以N开头" mat.Success);

            //以A开头,以ion结尾
            string myText2 = "Applal易做图slkdfasd;fasdfasdfasdfadsfadfadsfication";
            Match mat2 = Regex.Match(myText2, @"aS*ion", RegexOptions.IgnoreCase);
            Console.WriteLine(myText2 "是否以A开头Ion结尾,任意长度的单词" mat2.Success);

            Console.ReadLine();
            
        }
    }
}
补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,