asp.net中常用Regex應用執行個體

來源:互聯網
上載者:User
 代碼如下 複製代碼

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(); //嵌入條件之回溯匹配
        }

        /// <summary>
        /// 向前匹配
        /// </summary>
        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();
        }

        /// <summary>
        /// 向後匹配
        /// </summary>
        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();
        }

        /// <summary>
        /// 向前向後匹配結合
        /// </summary>
        static void LoopForwardAndAfterwardMatch()
        {
            StringBuilder sbStr = new StringBuilder();
            sbStr.Append("<Head>");
            sbStr.Append("rn ");
            sbStr.Append("<Title>Ben Forta's HomePage</Title>");
            sbStr.Append("rn");
            sbStr.Append("</Head>");

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

        /// <summary>
        /// 注意,這裡利用了回溯匹配法
        /// </summary>
        /// <param name="args"></param>
        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();
        }

        /// <summary>
        /// 嵌入條件,待匹配文本如下:
        /*********************************************************
         * <!-- Nav Bar -->
         * <TD>
         * <a href='/home'><img src='/images/home.gif'></a>
         * <img src=/images/spacer.gif'>
         * <a href='/search'><img src='/images/search.gif'></a>
         * <img src='/images/spacer.gif'>
         * <a href='/help'><img src='/images/help.gif'></a>
         * </td>
         *********************************************************/
        /// </summary>
        static void ConditionItems()
        {
            StringBuilder text = new StringBuilder();
            text.Append("<!-- Nav Bar -->");
            text.Append("rn");
            text.Append("<TD>");
            text.Append("rn");
            text.Append("<a href='/home'><img src='/images/home.gif'></a>");
            text.Append("rn");
            text.Append("<img src=/images/spacer.gif'>");
            text.Append("rn");
            text.Append("<a href='/search'><img src='/images/search.gif'></a>");
            text.Append("rn");
            text.Append("<img src='/images/spacer.gif'>");
            text.Append("rn");
            text.Append("<a href='/help'><img src='/images/help.gif'></a>");
            text.Append("rn");
            text.Append("</td>");

            string expression = @"(<as+[^>]+>s*)?<imgs+[^>]+>(?(1)s*</a>)";
            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();
        }
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.