日期:2009-04-23  浏览次数:20468 次

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

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


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