欢迎光临
我们一直在努力

正则表达式测试程序-.NET教程,.NET Framework

建站超值云服务器,限时71元/月

使用正则表达式提可以使我们灵活而又高效的处理文本。

正则表达式的全面模式匹配表示法可以使我们快速地分析大量的文本以找到特定的字符模式;提取、编辑、替换或删除文本子字符串;或将提取的字符串添加到集合以生成报告。

如果你对正则表达式还不了解,你可以看看以前发表的文章,有许多关于正则表达式入门的文章,本文不对正则表达式的使用进行介绍。本文主要给出了我自己做的一个正则表达式的测试例子,测试.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 : silverduck@eyou.com

*/

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 : silverduck@eyou.com

*/

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

}

}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 正则表达式测试程序-.NET教程,.NET Framework
分享到: 更多 (0)