Regex测试器

最近在使用正则表达式,所以看着书写了个Regex测试器,忘多多吐槽

图:

具体源码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
namespace Regex测试器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
txtShowStr.ReadOnly = true;
txtInputStr.ScrollBars = ScrollBars.Vertical;
txtReplaceStr.ScrollBars = ScrollBars.Vertical;
txtShowStr.ScrollBars = ScrollBars.Vertical;
}

/// <summary>
/// 该方法返回一个RegexOptions的值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// <returns></returns>
private RegexOptions getRegexOptions()
{
RegexOptions setRegexOptions =RegexOptions.None;//将RegexOptions初始值设置为None
if (this.cbECMAScript.Checked)
{
setRegexOptions |= RegexOptions.ECMAScript;
}
if (this.cbExplicitCapture.Checked)
{
setRegexOptions |= RegexOptions.ExplicitCapture;
}
if (this.cbIgnoreCase.Checked)
{
setRegexOptions |= RegexOptions.IgnoreCase;
}
if (this.cbIgnorePattern.Checked)
{
setRegexOptions |= RegexOptions.IgnorePatternWhitespace;
}
if (this.cbMultiline.Checked)
{
setRegexOptions |= RegexOptions.Multiline;
}
if (this.cbRightToLeft.Checked)
{
setRegexOptions |= RegexOptions.RightToLeft;
}
if (this.cbSingleline.Checked)
{
setRegexOptions |= RegexOptions.Singleline;
}
return setRegexOptions;
}
/// <summary>
/// 测试是否匹配
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnIsMatch_Click(object sender, EventArgs e)
{
try {
RegexOptions selectRegexOption = this.getRegexOptions();//得到选中的RegexOptions项
Regex testRegex = new Regex(txtRegexExpression.Text, selectRegexOption);
if (testRegex.IsMatch(txtInputStr.Text))
{
txtShowStr.ForeColor = Color.Black;
txtShowStr.Text = "匹配成功已经找到";
}
else
{
txtShowStr.ForeColor = Color.Red;
txtShowStr.Text = "匹配不成功,没有找到您需要的项";
}
}
catch(ArgumentException ex)
{
txtShowStr.ForeColor = Color.Red;
txtShowStr.Text = "匹配发生错误"+ex.Message;
}
}
/// <summary>
/// 替换方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnReplace_Click(object sender, EventArgs e)
{
try
{
RegexOptions selectRegexOptions = this.getRegexOptions();
Regex replaceRegex = new Regex(txtRegexExpression.Text, selectRegexOptions);
txtShowStr.ForeColor = Color.Black;
txtShowStr.Text = replaceRegex.Replace(txtInputStr.Text, txtReplaceStr.Text);//将替换的文本赋值给txtShowStr中
}
catch(ArgumentException ex)
{
txtShowStr.ForeColor = Color.Red;
txtShowStr.Text = "替换出错" + ex.Message;
}
}
/// <summary>
/// 分割方法
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSplit_Click(object sender, EventArgs e)
{
try
{
RegexOptions selectRegexOptions = getRegexOptions();
Regex SplitRegex = new Regex(txtRegexExpression.Text,selectRegexOptions);
string[] splitString = SplitRegex.Split(txtInputStr.Text);
StringBuilder sbResult = new StringBuilder(txtInputStr.Text.Length);
foreach (string splitElement in splitString)
{
sbResult.Append(splitElement + Environment.NewLine);
}
txtShowStr.ForeColor = Color.Black;
txtShowStr.Text = sbResult.ToString();
}
catch (ArgumentException ex)
{
txtShowStr.Text = "分割字符错误" + ex.Message;
}
}
/// <summary>
/// 匹配字符串
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnMatches_Click(object sender, EventArgs e)
{
try
{
RegexOptions selectRegexOptions = getRegexOptions();
Regex matchRegex = new Regex(txtRegexExpression.Text, selectRegexOptions);
MatchCollection matches = matchRegex.Matches(txtInputStr.Text);//得到匹配的集合
StringBuilder sbMatch = new StringBuilder(64);
string strNext = "-------下一个匹配----------";

foreach (Match matchElement in matches)

sbMatch.Append(matchElement.Value + Environment.NewLine + strNext+Environment.NewLine);
txtShowStr.ForeColor = Color.Black;
txtShowStr.Text = sbMatch.ToString();

}
catch (ArgumentException ex)
{
txtShowStr.ForeColor = Color.Red;
txtShowStr.Text = "匹配出错" + ex.Message;
}
}
/// <summary>
/// 重置操作使得所有的文本框为空
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnReset_Click(object sender, EventArgs e)
{
txtRegexExpression.Text = string.Empty;
txtInputStr.Text = string.Empty;
txtReplaceStr.Text = string.Empty;
txtShowStr.Text = string.Empty;
}
}
}

谢谢小工具类有需要此软件的可以联系我 qq:1171102882 非诚勿扰

 

posted @ 2013-03-11 16:29  罗导  阅读(251)  评论(0编辑  收藏  举报