一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

asp.net中常用正则表达式应用实例

时间:2011-12-09 编辑:简简单单 来源:一聚教程网


         *********************************************************/
        ///
        static void ConditionItems()
        {
            StringBuilder text = new StringBuilder();
            text.Append("");
            text.Append("rn");
            text.Append("");

            string expression = @"(]+>s*)?]+>(?(1)s*)";
            Regex reg = new Regex(expression, RegexOptions.IgnoreCase);
            MatchCollection mc = reg.Matches(text.ToString());
            //foreach (Match match in mc)
            //{
            //    Console.WriteLine(match.Value);
            //}

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine("the total count of match result is: " + mc.Count);
           
            for (int i = 0; i < mc.Count; i++)
            {
                Match match = mc[i];

                Console.ForegroundColor = ConsoleColor.Red;
               
                Console.WriteLine("|-Match["+i+"] is :"+match.Value);
               
                GroupCollection groups = match.Groups;
                for (int j = 0; j < groups.Count; j++)
                {
                    Console.ForegroundColor = ConsoleColor.DarkGreen;

                    Console.WriteLine("|---Groups[" + j + "] is :" + groups[j]);
                }
            }
            Console.ReadKey();
        }
    }
}

 代码如下 复制代码

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

namespace RegularExpressionConsoleApplication
{
    class Program
    {

      
        static void Main(string[] args)
        {
            //LoopBackMatch();  //回溯匹配(涉及子表达式)
            //LoopForwardMatch(); //向前匹配(?=)
            //LoopAfterwardMatch(); //向后匹配(?<=)
            //LoopForwardAndAfterwardMatch(); //向前向后匹配结合
            ConditionItems(); //嵌入条件之回溯匹配
        }

        ///


        /// 向前匹配
        ///

        static void LoopForwardMatch()
        {
            string text = " http://www.cnblogs.com";
            string expression = @".+(?=:)";
            Regex reg = new Regex(expression, RegexOptions.IgnoreCase);
            MatchCollection mc = reg.Matches(text);
            foreach (Match match in mc)
            {
                Console.WriteLine(match.Value);
            }

            Console.ReadKey();
        }

        ///


        /// 向后匹配
        ///

        static void LoopAfterwardMatch()
        {
            string text = "ABC01: $12.56";
            string expression = @"(?<=$)[0-9.]+";
            Regex reg = new Regex(expression, RegexOptions.IgnoreCase);
            MatchCollection mc = reg.Matches(text);
            foreach (Match match in mc)
            {
                Console.WriteLine(match.Value);
            }

            Console.ReadKey();
        }

        ///


        /// 向前向后匹配结合
        ///

        static void LoopForwardAndAfterwardMatch()
        {
            StringBuilder sbStr = new StringBuilder();
            sbStr.Append("");
            sbStr.Append("rn ");
            sbStr.Append("Ben Forta's HomePage");
            sbStr.Append("rn");
            sbStr.Append("");

            string expression = @"(?<=).*?(?=)";
            Regex reg = new Regex(expression, RegexOptions.IgnoreCase);
            MatchCollection mc = reg.Matches(sbStr.ToString());
            foreach (Match match in mc)
            {
                Console.WriteLine(match.Value);
            }
            Console.ReadKey();
        }

        ///


        /// 注意,这里利用了回溯匹配法
        ///

        ///
        static void LoopBackMatch()
        {
            string text = " this is  is a text, and I want to know   know if there is any repeated words in it.";
            string expression = @"[ ]+(w+)[ ]+1";
            Regex reg = new Regex(expression, RegexOptions.IgnoreCase);
            MatchCollection mc = reg.Matches(text);
            foreach (Match match in mc)
            {
                Console.WriteLine(match.Value);
            }

            Console.ReadKey();
        }

        ///


        /// 嵌入条件,待匹配文本如下:
        /*********************************************************
         *
         *

         *
         *
         *
         *
         *
         *
");
            text.Append("rn");
            text.Append("");
            text.Append("rn");
            text.Append("");
            text.Append("rn");
            text.Append("");
            text.Append("rn");
            text.Append("");
            text.Append("rn");
            text.Append("");
            text.Append("rn");
            text.Append("

热门栏目