使用正則表達式提可以使我們靈活而又高效的處理文本。
正則表達式的全面模式匹配表示法可以使我們快速地分析大量的文本以找到特定的字符模式;提取、編輯、替換或刪除文本子字符串;或將提取的字符串添加到集合以生成報告。
如果你對正則表達式還不了解,你可以看看以前發表的文章,有許多關于正則表達式入門的文章,本文不對正則表達式的使用進行介紹。本文主要給出了我自己做的一個正則表達式的測試例子,測試.net下的正則表達式。
由于我前一段時間總和正則表達式打交道,參考了不少前面的文章,也從網友的那里解決了不少問題,所以向回報一下大家。和寫完一個程序我們總要測試一下一樣,每寫一個正則表達式我也總是要測試以下它的正確性,使用vs的單步跟蹤能力可以對自己寫的式子逐個測試,可是效率太低。于是自己就寫了一個測試程序(使用c#開發,vs2003)暫時定名為regextester,下面是界面的一個截圖:
界面參考了一本書上的一個例子,是不是還比較容易理解。
在窗體的右下角有regex對應的各種方法的按鈕,點擊相應按鈕你就可以得到相應的結果。
在程序中我采用了treeview控件用來顯示match和matches的層次結構。對于replace和split的結果采用文本框顯示。treeview控件和文本框控件在同一個位置,通過相應的顯隱來顯示相應的結果。
你覺得:[1-4[a-b]hj]abc它能匹配什么?
2/d{3}(0[1-9])|(1[12])能正確找到類似200404這樣的年月組合嗎?
如果你不確定就要該程序測試一下把!
哈哈,通過該程序你也可以檢查論壇中網友給的結果是否正確。
 
由于我不知道程序該保存到那里,所以就把源代碼全貼出來了,源代碼分為兩個文件frmregextester為窗體文件。regextest為進行處理的類文件。
下面全是這兩個文件的代碼,注釋比較清楚,歡迎多提意見!
 
/* 程序名 : regextester
 * 用途 : .net下的正則表達式測試程序
 * 文件名 : frmregextester.cs
 * 作者 : silverduck
 * 日期 : 04/05/2004
 * e-mail : [email protected]
 */
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
using system.text.regularexpressions;
namespace regextester
{
 /// 
 /// 測試程序窗體
 /// 
 public class frmregextester : system.windows.forms.form
 {
 private system.windows.forms.button btnopenptn;
 private system.windows.forms.label label5;
 private system.windows.forms.textbox txtpattern;
 private system.windows.forms.groupbox grpregoption;
 private system.windows.forms.openfiledialog dlgopen;
 private system.windows.forms.savefiledialog dlgsave;
 private system.windows.forms.checkbox chkignorecase;
 private system.windows.forms.checkbox chkecmascript;
 private system.windows.forms.checkbox chkmultiline;
 private system.windows.forms.checkbox chksingleline;
 private system.windows.forms.checkbox chkignorepatternwhitespace;
 private system.windows.forms.checkbox chkcultureinvariant;
 private system.windows.forms.checkbox chkrighttoleft;
 private system.windows.forms.checkbox chkcompiled;
 private system.windows.forms.checkbox chkexplicitcapture;
 private system.windows.forms.label label6;
 private system.windows.forms.textbox txttest;
 private system.windows.forms.panel pnltest;
 private system.windows.forms.panel pnlresulttv;
 private system.windows.forms.treeview tvmatch;
 private system.windows.forms.label label1;
 private system.windows.forms.groupbox groupbox1;
 private system.windows.forms.button btnsaveptn;
 private system.windows.forms.button btnopeninput;
 private system.windows.forms.button btnexpand;
 private system.windows.forms.button btnismatch;
 private system.windows.forms.button btnmatch;
 private system.windows.forms.button btnsplit;
 private system.windows.forms.button btnreplace;
 private system.windows.forms.button btnmatches;
 private system.windows.forms.label label2;
 private system.windows.forms.textbox txtresult;
 private system.windows.forms.panel pnlresulttxt;
 private system.windows.forms.textbox txtreplacement;
 private system.windows.forms.button btnsaveresult;
 /// 
 /// 必需的設計器變量。
 /// 
 private system.componentmodel.container components = null;
 public frmregextester()
 {
 
 initializecomponent();
 
 }
 /// 
 /// 清理所有正在使用的資源。
 /// 
 protected override void dispose( bool disposing )
 {
 if( disposing )
 {
 if (components != null) 
 {
 components.dispose();
 }
 }
 base.dispose( disposing );
 }
 #region windows 窗體設計器生成的代碼
 /// 
 /// 設計器支持所需的方法 - 不要使用代碼編輯器修改
 /// 此方法的內容。
 /// 
 private void initializecomponent()
 {
 this.btnsaveptn = new system.windows.forms.button();
 this.btnopenptn = new system.windows.forms.button();
 this.label5 = new system.windows.forms.label();
 this.btnismatch = new system.windows.forms.button();
 this.txtpattern = new system.windows.forms.textbox();
 this.grpregoption = new system.windows.forms.groupbox();
 this.chkignorepatternwhitespace = new system.windows.forms.checkbox();
 this.chkcultureinvariant = new system.windows.forms.checkbox();
 this.chkrighttoleft = new system.windows.forms.checkbox();
 this.chkcompiled = new system.windows.forms.checkbox();
 this.chksingleline = new system.windows.forms.checkbox();
 this.chkmultiline = new system.windows.forms.checkbox();
 this.chkexplicitcapture = new system.windows.forms.checkbox();
 this.chkecmascript = new system.windows.forms.checkbox();
 this.chkignorecase = new system.windows.forms.checkbox();
 this.dlgopen = new system.windows.forms.openfiledialog();
 this.dlgsave = new system.windows.forms.savefiledialog();
 this.pnltest = new system.windows.forms.panel();
 this.btnopeninput = new system.windows.forms.button();
 this.label6 = new system.windows.forms.label();
 this.txttest = new system.windows.forms.textbox();
 this.pnlresulttv = new system.windows.forms.panel();
 this.btnexpand = new system.windows.forms.button();
 this.label1 = new system.windows.forms.label();
 this.tvmatch = new system.windows.forms.treeview();
 this.btnmatch = new system.windows.forms.button();
 this.btnsplit = new system.windows.forms.button();
 this.btnreplace = new system.windows.forms.button();
 this.btnmatches = new system.windows.forms.button();
 this.groupbox1 = new system.windows.forms.groupbox();
 this.txtreplacement = new system.windows.forms.textbox();
 this.pnlresulttxt = new system.windows.forms.panel();
 this.btnsaveresult = new system.windows.forms.button();
 this.txtresult = new system.windows.forms.textbox();
 this.label2 = new system.windows.forms.label();
 this.grpregoption.suspendlayout();
 this.pnltest.suspendlayout();
 this.pnlresulttv.suspendlayout();
 this.groupbox1.suspendlayout();
 this.pnlresulttxt.suspendlayout();
 this.suspendlayout();
 // 
 // btnsaveptn
 // 
 this.btnsaveptn.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.top | system.windows.forms.anchorstyles.right)));
 this.btnsaveptn.location = new system.drawing.point(584, 8);
 this.btnsaveptn.name = "btnsaveptn";
 this.btnsaveptn.size = new system.drawing.size(88, 23);
 this.btnsaveptn.tabindex = 2;
 this.btnsaveptn.text = "保存匹配模式";
 this.btnsaveptn.click += new system.eventhandler(this.btnsaveptn_click);
 // 
 // btnopenptn
 // 
 this.btnopenptn.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.top | system.windows.forms.anchorstyles.right)));
 this.btnopenptn.location = new system.drawing.point(456, 8);
 this.btnopenptn.name = "btnopenptn";
 this.btnopenptn.size = new system.drawing.size(96, 23);
 this.btnopenptn.tabindex = 1;
 this.btnopenptn.text = "從文件導入(&p)";
 this.btnopenptn.click += new system.eventhandler(this.btnopenptn_click);
 // 
 // label5
 // 
 this.label5.location = new system.drawing.point(8, 8);
 this.label5.name = "label5";
 this.label5.size = new system.drawing.size(112, 16);
 this.label5.tabindex = 0;
 this.label5.text = "pattern模式字符串";
 // 
 // btnismatch
 // 
 this.btnismatch.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.bottom | system.windows.forms.anchorstyles.right)));
 this.btnismatch.location = new system.drawing.point(241, 648);
 this.btnismatch.name = "btnismatch";
 this.btnismatch.tabindex = 7;
 this.btnismatch.text = "ismatch(&i)";
 this.btnismatch.click += new system.eventhandler(this.btnismatch_click);
 // 
 // txtpattern
 // 
 this.txtpattern.anchor = ((system.windows.forms.anchorstyles)(((system.windows.forms.anchorstyles.top | system.windows.forms.anchorstyles.left) 
 | system.windows.forms.anchorstyles.right)));
 this.txtpattern.location = new system.drawing.point(8, 32);
 this.txtpattern.maxlength = 32767000;
 this.txtpattern.multiline = true;
 this.txtpattern.name = "txtpattern";
 this.txtpattern.scrollbars = system.windows.forms.scrollbars.vertical;
 this.txtpattern.size = new system.drawing.size(672, 56);
 this.txtpattern.tabindex = 3;
 this.txtpattern.text = "";
 // 
 // grpregoption
 // 
 this.grpregoption.anchor = ((system.windows.forms.anchorstyles)(((system.windows.forms.anchorstyles.top | system.windows.forms.anchorstyles.left) 
 | system.windows.forms.anchorstyles.right)));
 this.grpregoption.controls.add(this.chkignorepatternwhitespace);
 this.grpregoption.controls.add(this.chkcultureinvariant);
 this.grpregoption.controls.add(this.chkrighttoleft);
 this.grpregoption.controls.add(this.chkcompiled);
 this.grpregoption.controls.add(this.chksingleline);
 this.grpregoption.controls.add(this.chkmultiline);
 this.grpregoption.controls.add(this.chkexplicitcapture);
 this.grpregoption.controls.add(this.chkecmascript);
 this.grpregoption.controls.add(this.chkignorecase);
 this.grpregoption.location = new system.drawing.point(8, 88);
 this.grpregoption.name = "grpregoption";
 this.grpregoption.size = new system.drawing.size(672, 66);
 this.grpregoption.tabindex = 4;
 this.grpregoption.tabstop = false;
 this.grpregoption.text = "regexoptions(正則表達式選項)";
 // 
 // chkignorepatternwhitespace
 // 
 this.chkignorepatternwhitespace.checked = true;
 this.chkignorepatternwhitespace.checkstate = system.windows.forms.checkstate.checked;
 this.chkignorepatternwhitespace.location = new system.drawing.point(400, 40);
 this.chkignorepatternwhitespace.name = "chkignorepatternwhitespace";
 this.chkignorepatternwhitespace.size = new system.drawing.size(168, 24);
 this.chkignorepatternwhitespace.tabindex = 8;
 this.chkignorepatternwhitespace.text = "ignorepatternwhitespace";
 // 
 // chkcultureinvariant
 // 
 this.chkcultureinvariant.location = new system.drawing.point(272, 40);
 this.chkcultureinvariant.name = "chkcultureinvariant";
 this.chkcultureinvariant.size = new system.drawing.size(128, 24);
 this.chkcultureinvariant.tabindex = 7;
 this.chkcultureinvariant.text = "cultureinvariant";
 // 
 // chkrighttoleft
 // 
 this.chkrighttoleft.location = new system.drawing.point(144, 40);
 this.chkrighttoleft.name = "chkrighttoleft";
 this.chkrighttoleft.size = new system.drawing.size(120, 24);
 this.chkrighttoleft.tabindex = 6;
 this.chkrighttoleft.text = "righttoleft";
 // 
 // chkcompiled
 // 
 this.chkcompiled.location = new system.drawing.point(16, 40);
 this.chkcompiled.name = "chkcompiled";
 this.chkcompiled.tabindex = 5;
 this.chkcompiled.text = "compiled ";
 // 
 // chksingleline
 // 
 this.chksingleline.location = new system.drawing.point(400, 16);
 this.chksingleline.name = "chksingleline";
 this.chksingleline.size = new system.drawing.size(120, 24);
 this.chksingleline.tabindex = 3;
 this.chksingleline.text = "singleline";
 // 
 // chkmultiline
 // 
 this.chkmultiline.location = new system.drawing.point(272, 16);
 this.chkmultiline.name = "chkmultiline";
 this.chkmultiline.size = new system.drawing.size(120, 24);
 this.chkmultiline.tabindex = 2;
 this.chkmultiline.text = "multiline";
 // 
 // chkexplicitcapture
 // 
 this.chkexplicitcapture.location = new system.drawing.point(144, 16);
 this.chkexplicitcapture.name = "chkexplicitcapture";
 this.chkexplicitcapture.size = new system.drawing.size(120, 24);
 this.chkexplicitcapture.tabindex = 1;
 this.chkexplicitcapture.text = "explicitcapture";
 // 
 // chkecmascript
 // 
 this.chkecmascript.location = new system.drawing.point(528, 16);
 this.chkecmascript.name = "chkecmascript";
 this.chkecmascript.size = new system.drawing.size(88, 24);
 this.chkecmascript.tabindex = 4;
 this.chkecmascript.text = "ecmascript";
 // 
 // chkignorecase
 // 
 this.chkignorecase.checked = true;
 this.chkignorecase.checkstate = system.windows.forms.checkstate.checked;
 this.chkignorecase.location = new system.drawing.point(16, 16);
 this.chkignorecase.name = "chkignorecase";
 this.chkignorecase.size = new system.drawing.size(120, 24);
 this.chkignorecase.tabindex = 0;
 this.chkignorecase.text = "ignorecase";
 // 
 // pnltest
 // 
 this.pnltest.anchor = ((system.windows.forms.anchorstyles)(((system.windows.forms.anchorstyles.top | system.windows.forms.anchorstyles.left) 
 | system.windows.forms.anchorstyles.right)));
 this.pnltest.controls.add(this.btnopeninput);
 this.pnltest.controls.add(this.label6);
 this.pnltest.controls.add(this.txttest);
 this.pnltest.location = new system.drawing.point(8, 161);
 this.pnltest.name = "pnltest";
 this.pnltest.size = new system.drawing.size(672, 247);
 this.pnltest.tabindex = 9;
 // 
 // btnopeninput
 // 
 this.btnopeninput.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.top | system.windows.forms.anchorstyles.right)));
 this.btnopeninput.location = new system.drawing.point(553, 4);
 this.btnopeninput.name = "btnopeninput";
 this.btnopeninput.size = new system.drawing.size(96, 23);
 this.btnopeninput.tabindex = 9;
 this.btnopeninput.text = "從文件導入(&f)";
 this.btnopeninput.click += new system.eventhandler(this.btnopeninput_click);
 // 
 // label6
 // 
 this.label6.location = new system.drawing.point(8, 8);
 this.label6.name = "label6";
 this.label6.size = new system.drawing.size(80, 16);
 this.label6.tabindex = 7;
 this.label6.text = "測試字符串";
 // 
 // txttest
 // 
 this.txttest.dock = system.windows.forms.dockstyle.bottom;
 this.txttest.location = new system.drawing.point(0, 31);
 this.txttest.multiline = true;
 this.txttest.name = "txttest";
 this.txttest.scrollbars = system.windows.forms.scrollbars.both;
 this.txttest.size = new system.drawing.size(672, 216);
 this.txttest.tabindex = 8;
 this.txttest.text = "";
 // 
 // pnlresulttv
 // 
 this.pnlresulttv.anchor = ((system.windows.forms.anchorstyles)((((system.windows.forms.anchorstyles.top | system.windows.forms.anchorstyles.bottom) 
 | system.windows.forms.anchorstyles.left) 
 | system.windows.forms.anchorstyles.right)));
 this.pnlresulttv.controls.add(this.btnexpand);
 this.pnlresulttv.controls.add(this.label1);
 this.pnlresulttv.controls.add(this.tvmatch);
 this.pnlresulttv.location = new system.drawing.point(8, 472);
 this.pnlresulttv.name = "pnlresulttv";
 this.pnlresulttv.size = new system.drawing.size(672, 168);
 this.pnlresulttv.tabindex = 9;
 // 
 // btnexpand
 // 
 this.btnexpand.location = new system.drawing.point(552, 0);
 this.btnexpand.name = "btnexpand";
 this.btnexpand.size = new system.drawing.size(112, 23);
 this.btnexpand.tabindex = 11;
 this.btnexpand.text = "展開所有內容(&e)";
 this.btnexpand.click += new system.eventhandler(this.btnexpand_click);
 // 
 // label1
 // 
 this.label1.location = new system.drawing.point(8, 8);
 this.label1.name = "label1";
 this.label1.size = new system.drawing.size(56, 16);
 this.label1.tabindex = 10;
 this.label1.text = "匹配結果";
 // 
 // tvmatch
 // 
 this.tvmatch.anchor = ((system.windows.forms.anchorstyles)((((system.windows.forms.anchorstyles.top | system.windows.forms.anchorstyles.bottom) 
 | system.windows.forms.anchorstyles.left) 
 | system.windows.forms.anchorstyles.right)));
 this.tvmatch.imageindex = -1;
 this.tvmatch.location = new system.drawing.point(0, 24);
 this.tvmatch.name = "tvmatch";
 this.tvmatch.selectedimageindex = -1;
 this.tvmatch.size = new system.drawing.size(672, 144);
 this.tvmatch.tabindex = 9;
 // 
 // btnmatch
 // 
 this.btnmatch.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.bottom | system.windows.forms.anchorstyles.right)));
 this.btnmatch.location = new system.drawing.point(337, 648);
 this.btnmatch.name = "btnmatch";
 this.btnmatch.tabindex = 10;
 this.btnmatch.text = "match(&m)";
 this.btnmatch.click += new system.eventhandler(this.btnmatch_click);
 // 
 // btnsplit
 // 
 this.btnsplit.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.bottom | system.windows.forms.anchorstyles.right)));
 this.btnsplit.location = new system.drawing.point(433, 648);
 this.btnsplit.name = "btnsplit";
 this.btnsplit.tabindex = 11;
 this.btnsplit.text = "split(&s)";
 this.btnsplit.click += new system.eventhandler(this.btnsplit_click);
 // 
 // btnreplace
 // 
 this.btnreplace.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.bottom | system.windows.forms.anchorstyles.right)));
 this.btnreplace.location = new system.drawing.point(521, 648);
 this.btnreplace.name = "btnreplace";
 this.btnreplace.tabindex = 12;
 this.btnreplace.text = "replace(&r)";
 this.btnreplace.click += new system.eventhandler(this.btnreplace_click);
 // 
 // btnmatches
 // 
 this.btnmatches.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.bottom | system.windows.forms.anchorstyles.right)));
 this.btnmatches.location = new system.drawing.point(608, 648);
 this.btnmatches.name = "btnmatches";
 this.btnmatches.tabindex = 13;
 this.btnmatches.text = "matches(&a)";
 this.btnmatches.click += new system.eventhandler(this.btnmatches_click);
 // 
 // groupbox1
 // 
 this.groupbox1.anchor = ((system.windows.forms.anchorstyles)(((system.windows.forms.anchorstyles.top | system.windows.forms.anchorstyles.left) 
 | system.windows.forms.anchorstyles.right)));
 this.groupbox1.controls.add(this.txtreplacement);
 this.groupbox1.location = new system.drawing.point(8, 416);
 this.groupbox1.name = "groupbox1";
 this.groupbox1.size = new system.drawing.size(672, 56);
 this.groupbox1.tabindex = 14;
 this.groupbox1.tabstop = false;
 this.groupbox1.text = "替換字符串";
 // 
 // txtreplacement
 // 
 this.txtreplacement.dock = system.windows.forms.dockstyle.fill;
 this.txtreplacement.location = new system.drawing.point(3, 17);
 this.txtreplacement.multiline = true;
 this.txtreplacement.name = "txtreplacement";
 this.txtreplacement.scrollbars = system.windows.forms.scrollbars.both;
 this.txtreplacement.size = new system.drawing.size(666, 36);
 this.txtreplacement.tabindex = 9;
 this.txtreplacement.text = "";
 // 
 // pnlresulttxt
 // 
 this.pnlresulttxt.anchor = ((system.windows.forms.anchorstyles)((((system.windows.forms.anchorstyles.top | system.windows.forms.anchorstyles.bottom) 
 | system.windows.forms.anchorstyles.left) 
 | system.windows.forms.anchorstyles.right)));
 this.pnlresulttxt.controls.add(this.btnsaveresult);
 this.pnlresulttxt.controls.add(this.txtresult);
 this.pnlresulttxt.controls.add(this.label2);
 this.pnlresulttxt.location = new system.drawing.point(8, 472);
 this.pnlresulttxt.name = "pnlresulttxt";
 this.pnlresulttxt.size = new system.drawing.size(672, 168);
 this.pnlresulttxt.tabindex = 15;
 // 
 // btnsaveresult
 // 
 this.btnsaveresult.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.top | system.windows.forms.anchorstyles.right)));
 this.btnsaveresult.location = new system.drawing.point(560, 0);
 this.btnsaveresult.name = "btnsaveresult";
 this.btnsaveresult.size = new system.drawing.size(88, 23);
 this.btnsaveresult.tabindex = 12;
 this.btnsaveresult.text = "保存匹配結果";
 this.btnsaveresult.click += new system.eventhandler(this.btnsaveresult_click);
 // 
 // txtresult
 // 
 this.txtresult.anchor = ((system.windows.forms.anchorstyles)((((system.windows.forms.anchorstyles.top | system.windows.forms.anchorstyles.bottom) 
 | system.windows.forms.anchorstyles.left) 
 | system.windows.forms.anchorstyles.right)));
 this.txtresult.location = new system.drawing.point(0, 24);
 this.txtresult.multiline = true;
 this.txtresult.name = "txtresult";
 this.txtresult.readonly = true;
 this.txtresult.scrollbars = system.windows.forms.scrollbars.both;
 this.txtresult.size = new system.drawing.size(672, 144);
 this.txtresult.tabindex = 11;
 this.txtresult.text = "";
 // 
 // label2
 // 
 this.label2.location = new system.drawing.point(8, 8);
 this.label2.name = "label2";
 this.label2.size = new system.drawing.size(56, 16);
 this.label2.tabindex = 10;
 this.label2.text = "匹配結果";
 // 
 // frmregextester
 // 
 this.autoscalebasesize = new system.drawing.size(6, 14);
 this.clientsize = new system.drawing.size(688, 677);
 this.controls.add(this.pnlresulttxt);
 this.controls.add(this.groupbox1);
 this.controls.add(this.btnmatches);
 this.controls.add(this.btnreplace);
 this.controls.add(this.btnsplit);
 this.controls.add(this.btnmatch);
 this.controls.add(this.pnltest);
 this.controls.add(this.grpregoption);
 this.controls.add(this.btnsaveptn);
 this.controls.add(this.btnopenptn);
 this.controls.add(this.label5);
 this.controls.add(this.btnismatch);
 this.controls.add(this.txtpattern);
 this.controls.add(this.pnlresulttv);
 this.minimumsize = new system.drawing.size(650, 600);
 this.name = "frmregextester";
 this.text = "正則表達式測試器";
 this.grpregoption.resumelayout(false);
 this.pnltest.resumelayout(false);
 this.pnlresulttv.resumelayout(false);
 this.groupbox1.resumelayout(false);
 this.pnlresulttxt.resumelayout(false);
 this.resumelayout(false);
 }
 #endregion
 /// 
 /// 應用程序的主入口點。
 /// 
 [stathread]
 static void main() 
 {
 application.run(new frmregextester());
 }
 #region 自定義函數
 
 //從指定文件中得到匹配字符串
 private void btnopenptn_click(object sender, system.eventargs e) {
 try{
 if(dlgopen.showdialog() == dialogresult.ok ){
 txtpattern.text= regextest.getcontent(dlgopen.filename);
 }
 }catch( exception ex){
 messagebox.show(ex.message,"文件操作錯誤");
 }
 }
 
 //把匹配模式保存到文件中,使用append方式
 private void btnsaveptn_click(object sender, system.eventargs e) {
 try{
 if( dlgsave.showdialog() == dialogresult.ok){
 regextest.savecontent(dlgsave.filename,txtpattern.text);
 }
 }catch( exception ex){
 messagebox.show(ex.message,"錯誤");
 }
 }
 //保存匹配結果
 private void btnsaveresult_click(object sender, system.eventargs e) {
 if( txtresult.text ==""){
 messagebox.show("匹配結果內容為空/n不能保存","錯誤");
 return;
 }
 }
 //從指定文件中得到要匹配的內容
 private void btnopeninput_click(object sender, system.eventargs e) {
 try{
 if(dlgopen.showdialog() == dialogresult.ok ){
 txttest.text= regextest.getcontent(dlgopen.filename);
 }
 }catch( exception ex){
 messagebox.show(ex.message,"文件操作錯誤");
 }
 }
 //擴展所有的treeview控件內容
 private void btnexpand_click(object sender, system.eventargs e) {
 this.tvmatch.expandall();
 }
 //得到指定的regexoptions選項
 private regexoptions getregexoptions(){
 regexoptions regopt = regexoptions.none;
 if( chkcompiled.checked == true) regopt |= regexoptions.compiled;
 if( chkcultureinvariant.checked == true) regopt |= regexoptions.cultureinvariant;
 if( chkecmascript.checked == true) regopt |= regexoptions.ecmascript;
 if( chkexplicitcapture.checked == true) regopt |= regexoptions.explicitcapture;
 if( chkignorecase.checked == true) regopt |= regexoptions.ignorecase;
 if( chkignorepatternwhitespace.checked==true) regopt |= regexoptions.ignorepatternwhitespace;
 if( chkmultiline.checked == true) regopt |= regexoptions.multiline;
 if( chkrighttoleft.checked == true) regopt |= regexoptions.righttoleft;
 if( chksingleline.checked == true) regopt |= regexoptions.singleline;
 return regopt; 
 }
 //是否匹配
 private void btnismatch_click(object sender, system.eventargs e) {
 if( regextest.ismatch(txtpattern.text,txttest.text,this.getregexoptions())){
 messagebox.show("匹配成功!","匹配成功");
 }else{
 messagebox.show("匹配不成功/n你也許需要修改pattern","匹配不成功");
 }
 }
 //只進行一次匹配測試
 private void btnmatch_click(object sender, system.eventargs e) {
 pnlresulttxt.visible= false;
 pnlresulttv.visible = true;
 this.tvmatch.nodes.clear();
 regextest regtest = new regextest(this.getregexoptions());
 regtest.pattern = txtpattern.text;
 regtest.input = txttest.text;
 regtest.match(this.tvmatch);
 
 }
 //字符串分割
 private void btnsplit_click(object sender, system.eventargs e) {
 pnlresulttv.visible = false;
 pnlresulttxt.visible = true;
 txtresult.text = "";
 regextest regtest = new regextest(this.getregexoptions());
 regtest.pattern = txtpattern.text;
 regtest.input = txttest.text;
 regtest.split(txtresult);
 }
 //字符串替換
 private void btnreplace_click(object sender, system.eventargs e) {
 pnlresulttv.visible = false;
 pnlresulttxt.visible = true;
 txtresult.text = "";
 regextest regtest = new regextest(this.getregexoptions());
 regtest.pattern = txtpattern.text;
 regtest.input = txttest.text;
 regtest.replace(txtresult,txtreplacement.text);
 }
 //得到所有匹配組
 private void btnmatches_click(object sender, system.eventargs e) {
 
 pnlresulttxt.visible= false;
 pnlresulttv.visible = true;
 this.tvmatch.nodes.clear();
 regextest regtest = new regextest(this.getregexoptions());
 regtest.pattern = txtpattern.text;
 regtest.input = txttest.text;
 regtest.matches(this.tvmatch);
 }
 #endregion
 
 
 }
}
regextest類代碼:
/* 程序名 : regextester
 * 用途 : .net下的正則表達式測試程序
 * 文件名 : regextest.cs
 * 作者 : silverduck
 * 日期 : 04/05/2004
 * e-mail : [email protected]
 */
using system;
using system.io;
using system.windows.forms;
using system.text.regularexpressions;
namespace regextester
{
 /// 
 /// 正則表達式測試類。用于測試各種正則表達式。
 /// 
 public class regextest {
 #region 私有變量
 
 
 private string pattern; //模式字符串
 private string input; //輸入字符串
 private regex rgx; //正則表帶式
 private regexoptions rgxopt; //正則表達式選項
 
 
 #endregion 私有變量
 #region 公有屬性
 /// 
 /// 匹配模式字符串
 /// 
 public string pattern{
 get{ return pattern;}
 set{ pattern = value;}
 }
 /// 
 /// 輸入的要匹配的字符串
 /// 
 public string input{
 get{ return input;}
 set{ input = value;}
 }
 /// 
 /// 正則表達式選項
 /// 
 public regexoptions rgxoptions{
 get{return rgxopt;}
 set{rgxopt = value;}
 }
 
 #endregion 公有屬性
 #region 構造函數
 
 /// 
 /// 正則表達式測試類構造函數
 /// 
 ///正則表達式選項
 public regextest(regexoptions rgxopt):this(rgxopt,"",""){ }
 /// 
 /// 正則表達式測試類構造函數
 /// 
 ///正則表達式選項
 ///模式匹配字符串
 ///輸入字符串
 public regextest(regexoptions rgxopt,string pattern,string input){
 this.pattern = pattern;
 this.rgxopt = rgxopt;
 this.input = input;
 rgx = null;
 
 }
 
 #endregion 構造函數
 #region 私有方法
 /// 
 /// 得到正則表達式類
 /// 
 private void createregex(){
 if( pattern != "" && input != ""){
 rgx = new regex(pattern,rgxopt);
 }
 }
 /// 
 /// 設置顯示匹配結果的treeview控件
 /// 
 /// 顯示匹配結果的treeview控件
 /* 在treeview控件中顯示的內容格式:
 ╠匹配成功
 ╚match數目 
 ╠match-0
 ╠結果
 ╠group數目
 ╠match屬性index
 ╠match屬性length
 ╠match屬性value
 ╠group-0
 ╠結果
 ╠capture數目
 ╠group屬性index
 ╠group屬性length
 ╠group屬性value
 ╠capture-0
 ╠capture屬性index
 ╠capture屬性length
 ╠...
 ╠capture-1
 ╠capture屬性
 ╠capture屬性
 ╠...
 ╠...
 ╠capture-n 
 ╠group-1
 ╠結果
 ╠...
 ╠capture-0
 ╠capture屬性
 ╠capture屬性
 ╠...
 ╠...
 ╠capture-n 
 ╠...
 ╠group-n
 ╠match-1
 ╠結果
 ╠...
 ╠group-0
 ...
 ╠group-n
 ...
 ╠match-n 
 */
 private void setshowresulttreeview(matchcollection amatch,treeview tvshowresult){
 //指定的treeview控件不為空則向其中寫入值
 //否則不作任何處理
 if( tvshowresult != null && amatch != null ){
 tvshowresult.beginupdate(); 
 tvshowresult.nodes.clear();
 //寫入捕獲結果總體信息
 if( amatch.count > 0){
 
 tvshowresult.nodes.add("匹配成功");
 tvshowresult.nodes[0].nodes.add("match數—"+amatch.count.tostring());
 for( int i=0;i < amatch.count && amatch[i].success;i++ ){
 treenode tn = new treenode("match - " + i.tostring());
 this.addmatch(tn,amatch[i]);
 tvshowresult.nodes.add(tn);
 }
 }
 //寫入各個捕獲組
 tvshowresult.endupdate();
 } 
 }
 /// 
 /// 向treenode中插入匹配所得的match
 /// 
 /// 要向其中插入內容的節點
 /// 要插入到節點中的match結果
 private void addmatch(treenode tn, match mth){
 tn.nodes.add("結果");
 tn.nodes[0].nodes.add("group 數 — "+mth.captures.count.tostring());
 tn.nodes[0].nodes.add("起始位置 — "+mth.index.tostring());
 tn.nodes[0].nodes.add("長 度 — "+mth.length.tostring());
 tn.nodes[0].nodes.add("捕 獲 值 — "+mth.value);
 
 for( int i=0;i < mth.groups.count;i++ ){
 treenode tnd = new treenode("group - " + i.tostring());
 this.addgroup(tnd,mth.groups[i]);
 tn.nodes.add(tnd);
 }
 }
 /// 
 /// 向treenode中插入匹配所得的group
 /// 
 /// 要向其中插入內容的節點
 /// 要插入到節點中的group結果
 private void addgroup(treenode tn, group grp){
 tn.nodes.add("結果");
 tn.nodes[0].nodes.add("capture數—"+grp.captures.count.tostring());
 tn.nodes[0].nodes.add("起始位置 — "+grp.index.tostring());
 tn.nodes[0].nodes.add("長 度 — "+grp.length.tostring());
 tn.nodes[0].nodes.add("捕 獲 值 — "+grp.value);
 
 for( int i=0;i < grp.captures.count;i++ ){
 treenode tnd = new treenode("capture - " + i.tostring());
 this.addcapture(tnd,grp.captures[i]);
 tn.nodes.add(tnd);
 }
 }
 /// 
 /// 向treenode中插入匹配所得的capture
 /// 
 /// 要向其中插入內容的節點
 /// 要插入到節點中的capture結果
 private void addcapture(treenode tn, capture cpt){
 tn.nodes.add("起始位置 — "+cpt.index.tostring());
 tn.nodes.add("長 度 — "+cpt.length.tostring());
 tn.nodes.add("捕 獲 值 — "+cpt.value);
 
 }
 
 
 #endregion 私有方法
 #region 公有方法
 /// 
 /// 根據給定的匹配模式替換輸入的字符串
 /// 
 public void matches(treeview tv){
 matchcollection amatch;
 this.createregex();
 if( rgx != null){
 amatch=rgx.matches(input);
 this.setshowresulttreeview(amatch,tv);
 
 }
 }
 /// 
 /// 得到一次匹配結果
 /// 
 /// 顯示匹配結果的treeview控件
 public void match(treeview tv){
 this.createregex();
 
 if( rgx != null){
 match mth = rgx.match(input);
 if( mth.success){
 treenode tn= new treenode("匹配成功");
 tv.nodes.add(tn);
 this.addmatch(tn,mth);
 }
 }
 }
 /// 
 /// 根據給定的匹配模式分隔輸入的字符串
 /// 
 /// 要在其中顯示結果的文本框控件
 public void split(textbox txtresult){
 this.createregex();
 
 if( rgx != null){
 string[] astr = rgx.split(input);
 txtresult.appendtext("通過分割共得到"+astr.length.tostring()+"個字符串。它們分別為:"+system.environment.newline);
 foreach( string s in astr){
 txtresult.appendtext(s+system.environment.newline);
 }
 }
 }
 
 /// 
 /// 根據給定的匹配模式替換輸入的字符串
 /// 
 /// 要在其中顯示結果的文本框控件
 /// 替換的內容
 public void replace(textbox txtresult,string replacement){
 this.createregex();
 
 if( rgx != null){
 txtresult.appendtext( "替換結果為:"+ system.environment.newline);
 txtresult.appendtext(rgx.replace(input,replacement));
 }
 }
 /// 
 /// 是否匹配指定內容
 /// 
 public static bool ismatch(string pattern,string content,regexoptions regopt){
 return regex.ismatch(content,pattern,regopt);
 }
 
 /// 
 /// 從指定的文件中得到字符串
 /// 
 /// 要從中得到字符串的文件名
 /// 返回從文件中得到的字符串
 public static string getcontent(string filename){
 string content="";
 try{
 streamreader sr = new streamreader(filename,system.text.encoding.default,true);
 content = sr.readtoend();
 sr.close();
 }catch(exception ex){
 throw ex;
 }
 return content;
 } 
 
 /// 
 /// 把指定的內容存入到指定的文件名中
 /// 
 /// 要存入內容的文件名
 /// 要存入的內容
 public static void savecontent(string filename,string content){
 try{
 streamwriter sw = new streamwriter(filename,true,system.text.encoding.default);
 sw.write(content+system.environment.newline);
 sw.close();
 }catch(exception ex){
 throw ex;
 }
 }
 
 #endregion
 }
 
}