码迷,mamicode.com
首页 > Windows程序 > 详细

C#正则表达式

时间:2017-06-26 10:14:17      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:style   for   abc   match   yun   off   div   cost   方法   

下面我简要介绍一下C#中正则表达式的常用形式

常用的方法有:

Regex.IsMatch(value,pattern) 返回true 或者false

Match match = Regex.Match(input, pattern);

Regex.Replace(input,pattern,replacement)

Regex.Split(input,pattern)

具体使用详情还是看代码:

static void Main(string[] args)
        {
            //RegexIsMatch();
            //RegexMatch();
            //RegexReplace();
            //RegexSplit();
            Matches();
            Console.ReadLine();
        }
        /// <summary>
        /// 最基本的方法判断是否匹配
        /// </summary>
        static void RegexIsMatch()
        {
            string[] values = { "111-22-3333", "111-2-3333" };
            string pattern = @"^\d{3}-\d{2}-\d{4}$";
            foreach(var value in values)
            {
                if(Regex.IsMatch(value,pattern))
                {
                    Console.WriteLine("{0} is valid",value);
                }
                else
                {
                    Console.WriteLine("{0} is not valid", value);
                }
            }
           
        }

        /// <summary>
        /// match方法返回Match类型
        /// </summary>
        static void RegexMatch()
        {
            string input = "This is TianYun TianYun";
            string pattern = @"\b(\w+)\s(\1)\b";
            Match match = Regex.Match(input, pattern);
            while (match.Success)
            {
                Console.WriteLine("Duplication {0} found",match.Groups[0].Value);
                match = match.NextMatch();
            }
        }
        /// <summary>
        /// Replace method $& is the matched string,and the $$ is $
        /// </summary>
        static void RegexReplace()
        {
            string input = "Total cost:103.44";
            string pattern = @"\b\d+\.\d{2}\b";
            string replacement = "$$$&";
            Console.WriteLine(Regex.Replace(input,pattern,replacement));
        }
        /// <summary>
        /// split string by pattern, return array
        /// </summary>
        static void RegexSplit()
        {
            string input = "1. egg 2. bread 3. milk 4. coffee";
            string pattern = @"\b\d\.\s";
            foreach (var s in Regex.Split(input,pattern))
            {
                if (!string.IsNullOrEmpty(s))
                {
                    Console.WriteLine(s);
                }
            }
        }
        /// <summary>
        /// return Matches
        /// </summary>
        static void Matches()
        {
            MatchCollection matchs = Regex.Matches("abc123abc42abc54", @"abc");
            foreach (Match match in matchs)
            {
                Console.WriteLine("{0} is found at {1}",match.Value,match.Index);
                Console.WriteLine("{0}",match.Result("$& hello This is match.Result"));
            }
        }

 

 

C#正则表达式

标签:style   for   abc   match   yun   off   div   cost   方法   

原文地址:http://www.cnblogs.com/birdofparadise/p/7078861.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!