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(); } } } |