=============================================== private void WashTelNumList() { //用陣列儲存要測試的資料 string[] aryTelNum={"02-23939889","(02)23939889","02 23939889","0933123123","0933-123-123"}; for (int i=0; i0 || sCLTest.IndexOf(")")>0) { //取出區域碼 int i=sCLTest.IndexOf(")"); if (i>0) { //取出)之前的部分並去掉( sZonePart=sCLTest.Substring(0,i).Replace("(",""); sPhonePart=sCLTest.Substring(i+1); } else //使用-分隔區碼與電話碼的情形 { i=sCLTest.IndexOf("-"); sZonePart=sCLTest.Substring(0,i); sPhonePart=sCLTest.Substring(i+1); } } } sZonePart=sZonePart.Trim(); //區域去空白 sPhonePart=sPhonePart.Replace(" ",""); //電話號碼部分去除空白及- sPhonePart=sPhonePart.Replace("-",""); //檢查區碼是否為2-3碼純數字且第一碼為0 if (sZonePart.Length<2 || sZonePart.Length>3 || sZonePart.Substring(0,1)!="0" || !Microsoft.VisualBasic.Information.IsNumeric(sZonePart)) bCityLine=false; //檢查電話號碼部分是否為8或7碼純數字 if (sPhonePart.Length<7 || sPhonePart.Length>8 || !Microsoft.VisualBasic.Information.IsNumeric(sPhonePart)) bCityLine=false; if (bCityLine) return "C("+sZonePart+")"+sPhonePart; return "U"+sCLTest; } =============================================== ============================================ private void WashTelNumList() { //用陣列儲存要測試的資料 string[] aryTelNum={"02-23939889","(02)23939889","02 23939889","0933123123","0933-123-123"}; for (int i=0; i\d\d)-?(?\d{3})-?(?\d{3})", "M09${p1}${p2}${p3}"); } if (Regex.Match(PhoneNumber, @"[(]*\d{2,3}[-)\s]\d{2,4}-?\d{4}").Success) { string sTemp=Regex.Replace(PhoneNumber, @"[(]*(?\d{2,3})[-)\s](?\d{2,4})-?(?\d{4})", "(${zone})${p1}${p2}"); if (!Regex.Match(sTemp, @"[(]\d{2,3}[)]\d{7,8}").Success) return "U"+sTemp; else return "C"+sTemp; } return "U"+PhoneNumber; } ================================================== ======================================================== private void GroupSample() { string sSource="台北市110東興路59號6F"; //利用(?pattern)的語法, 將比對結果分組 Match match=Regex.Match(sSource,@"(?\D+?)(?\d{3})(?.+)"); if (match.Success) { MessageBox.Show("縣市="+match.Groups["city"].ToString()); MessageBox.Show("郵遞區號="+match.Groups["zipcode"].ToString()); MessageBox.Show("地址="+match.Groups["addr"].ToString()); } } ==================================================== ============================================================ //分群擷取功能測試 private void GroupTest() { string sSource=" Jeffrey, 1997/4/1, 0800956956 Sharon, 2001/11/4, 0933123123 Ryan 1999/12/31 (02)87682688"; foreach (Match match in Regex.Matches(sSource,@"(?[^ ,]+)[ ,]+(?[0-9/]+)[ ,]+(?[0-9()]+)")) { //將結果輸出在Debug的輸出視窗中 System.Diagnostics.Debug.WriteLine("Name=" + match.Groups["name"].ToString() + " Date="+match.Groups["date"].ToString() + " TelNo=" + match.Groups["tel"].ToString()); } } ==============================================================   ============================================ private ArrayList aryLinks=new ArrayList(); private string BaseURL=""; … Windows Forms固定程式碼部分省略 … private void btnOK_Click(object sender, System.EventArgs e) { //取出http://.../ 的部分作為BaseURL try { BaseURL=Regex.Match(txtURL.Text, "(?i)(?http://[^/]+?/)").Groups["baseurl"].ToString(); } catch { MessageBox.Show("起始網頁之URL格式應為http://www.xxx.com/!"); return; } const string ROOT="起始網頁"; treeView1.Nodes.Clear(); TreeNode RootNode=new TreeNode(ROOT); treeView1.Nodes.Add(RootNode); AnalyzeHyperlink(txtURL.Text,RootNode); } //此一函數可被遞迴呼叫 private void AnalyzeHyperlink(string URL,TreeNode ParentNode) { if (URL.Substring(0,1)=="/") //絕對路徑由BaseURL起算 { URL=BaseURL+URL; } else if (URL.IndexOf(":")<0) //排除mailto: http: ftp: javascript: ..., 其餘採相對路徑, 由Parent的所在位址起算 { URL=Regex.Match(ParentNode.Text, "(?i)(?http://.+/)").Groups["baseurl"].ToString()+URL; } string HTMLCode=""; if (URL.Substring(0,5).ToLower()=="http:") { HTMLCode=SketchWebPage(URL); if (HTMLCode=="FAILED") URL="(X)"+URL; //若無法取回網頁則在Text上標註(X) } else //非HTTP連結標註(?) { URL="(?)"+URL; } TreeNode NowNode=new TreeNode(URL); ParentNode.Nodes.Add(NowNode); foreach (Match match in Regex.Matches(HTMLCode, "(?i)[<](?\\w+)\\s[^>]*?(?src|href)=['\"](?.+?)[\"'].*?[>]")) { string HTMLTag=match.Groups["tag"].ToString().ToUpper(); if (!(HTMLTag=="A" || HTMLTag=="FRAME")) continue; //只限定FRAME與A Tag, 可視需要增刪 string Hyperlink=match.Groups["url"].ToString(); if (Regex.IsMatch(Hyperlink,"(?i)http://")) continue; //超連至其他網站者不處理 if (aryLinks.Contains(Hyperlink)) continue; //已處理過的就跳過 aryLinks.Add(Hyperlink); AnalyzeHyperlink(Hyperlink,NowNode); treeView1.Update(); } } //不錯用的函數, 給URL就傳回網頁內容 private string SketchWebPage(string URL) { HttpWebRequest reqPage=(HttpWebRequest) WebRequest.Create(URL); try { HttpWebResponse rspPage=(HttpWebResponse) reqPage.GetResponse(); Stream stmPage=rspPage.GetResponseStream(); StreamReader srPage=new StreamReader(stmPage,System.Text.Encoding.GetEncoding("big5")); string sTemp=srPage.ReadToEnd(); rspPage.Close(); stmPage.Close(); srPage.Close(); return sTemp; } catch { return "FAILED"; } } ================================================ ========================================== using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Text.RegularExpressions; namespace RegexTester { /// /// Summary description for Form1. /// public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.TextBox txtInput; private System.Windows.Forms.TextBox txtPattern; private System.Windows.Forms.Button button1; private System.Windows.Forms.ListBox lstResult; private System.Windows.Forms.Label label1; …Windows Form之通用程式碼部分省略… private void button1_Click(object sender, System.EventArgs e) { try { //先使用Regex解析Pattern是否有定義Group ArrayList aList=new ArrayList(); foreach (Match m in Regex.Matches(txtPattern.Text, "[(][?][<](?.+?)[>]")) { aList.Add(m.Groups["groupname"].Value); //將出現的Group名稱放入ArrayList中 } lstResult.Items.Clear(); int i=0; foreach (Match m in Regex.Matches(txtInput.Text,txtPattern.Text)) { i++; string sResult=i.ToString()+".∣"+m.Index.ToString()+"∣:"+m.Groups[0].Value; if (m.Groups.Count>1) { for (int j=0; j"; } } lstResult.Items.Add(sResult); } } catch (Exception ex) { MessageBox.Show("發生錯誤: "+ex.Message); } } //為加強互動性, 使用者點選某筆結果時, 可映對出在原文中的位置, 並形成反白 private void lstResult_SelectedIndexChanged(object sender, System.EventArgs e) { string sTemp=lstResult.Items[lstResult.SelectedIndex].ToString(); string[] aAnalysis=Regex.Split(sTemp,"∣"); int StartPos=int.Parse(aAnalysis[1]); txtInput.Focus(); txtInput.Select(StartPos,aAnalysis[2].Length-1); txtInput.ScrollToCaret(); } } } ========================================================