#眉標=.NET #副標=探索.NET Regular Expression元件 #大標=被遺忘的文字利器(下) #作者=文/李明儒 ============================================================ //分群擷取功能測試 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(); } } } ========================================================