也谈正则表达式
来源:互联网 发布:医学实验技术专业 知乎 编辑:程序博客网 时间:2024/05/16 18:14
2007年03月20日 21:38:00
其实很早就知道了正则表达式,在集成VBScript脚本的时候,就看到了该功能,不过那时觉得很难,觉得也派不上什么用场,所以也没有过多关注。
最近看了
前段时间买了本红皮书《C#字符串和正则表达式参考书》(这真是一本好书,想学习正则表达式的可以参考一下),花了几天的时间把该书看了一遍,正则表达式的用法基本上也弄清楚了,并且对字符串相关的知识也越来越感兴趣了。
对正则表达式的具体规则和使用,实没有什么可说的,网上的文章多的很,都说的比我的好。这是我学习正则表达式做的一个简单正则表达式测试工具,其实大部分代码就是上面书的一个示例(不知道为什么,上网竟没有找到该书的示例源码),又上网查了一些资料,把一些常见的正则表达式也嵌入了进去,方便了正则表达式的应用(以后有时间做一个比较理想的正则表达式工具)。
这是程序的截图:
相关源码:
//获取正则表达式的匹配参数
private RegexOptions GetSelectedRegexOptions()
{
RegexOptions selectedRegexOptions = RegexOptions.None;
if (this.IgnoreCaseChkBox.Checked)
selectedRegexOptions |= RegexOptions.IgnoreCase;
if (this.ExplicitCaptureChkBox.Checked)
selectedRegexOptions |= RegexOptions.ExplicitCapture;
if (this.ECMAScriptChkBox.Checked)
selectedRegexOptions |= RegexOptions.ECMAScript;
if (this.IgnoreCaseChkBox.Checked)
selectedRegexOptions |= RegexOptions.IgnoreCase;
if (this.MultiLineChkBox.Checked)
selectedRegexOptions |= RegexOptions.Multiline;
if (this.RightToLeftChkBox.Checked)
selectedRegexOptions |= RegexOptions.RightToLeft;
if (this.SingeLineChkBox.Checked)
selectedRegexOptions |= RegexOptions.Singleline;
return selectedRegexOptions;
}
private void TestRegexButton_Click(object sender, EventArgs e)
{
try
{
RegexOptions selectedRegexOptions = this.GetSelectedRegexOptions();
Regex testRegex = new Regex(this.RegexTextBox.Text, selectedRegexOptions);
if (testRegex.IsMatch(this.InputTextBox.Text))
{
this.ResultsTextBox.ForeColor = Color.Black;
this.ResultsTextBox.Text = "匹配成功";
}
else
{
this.ResultsTextBox.ForeColor = Color.Red;
this.ResultsTextBox.Text = "匹配失败";
}
}
catch (ArgumentException ex)
{
this.ResultsTextBox.ForeColor = Color.Red;
this.ResultsTextBox.Text = "错误:"+ex.Message;
}
}
private void ReplaceButton_Click(object sender, EventArgs e)
{
try
{
RegexOptions selectedRegexOptions = this.GetSelectedRegexOptions();
Regex replaceRegex = new Regex(this.RegexTextBox.Text, selectedRegexOptions);
this.ResultsTextBox.ForeColor = Color.Black;
this.ResultsTextBox.Text = replaceRegex.Replace(this.InputTextBox.Text, this.ReplacementTextBox.Text);
}
catch (ArgumentException ex)
{
this.ResultsTextBox.ForeColor = Color.Red;
this.ResultsTextBox.Text = "错误:" + ex.Message;
}
}
private void SplitBoutton_Click(object sender, EventArgs e)
{
try
{
RegexOptions selectedRegexOptions = this.GetSelectedRegexOptions();
Regex splitRegex = new Regex(this.RegexTextBox.Text, selectedRegexOptions);
String[] splitResults;
splitResults = splitRegex.Split(this.InputTextBox.Text);
StringBuilder resultsString = new StringBuilder(this.InputTextBox.Text.Length);
foreach (String stringElement in splitResults)
resultsString.Append(stringElement + Environment.NewLine);
this.ResultsTextBox.ForeColor = Color.Black;
this.ResultsTextBox.Text = resultsString.ToString();
}
catch (ArgumentException ex)
{
this.ResultsTextBox.ForeColor = Color.Red;
this.ResultsTextBox.Text = "错误:" + ex.Message;
}
}
private void MatchesButton_Click(object sender, EventArgs e)
{
try
{
RegexOptions selectedRegexOptions = this.GetSelectedRegexOptions();
Regex matchesRegex = new Regex(this.RegexTextBox.Text, selectedRegexOptions);
MatchCollection matchesFound;
matchesFound = matchesRegex.Matches(this.InputTextBox.Text);
String nextMath = "------- 下一个匹配项 ---------- ";
StringBuilder resultsString = new StringBuilder(64);
foreach(Match matchMode in matchesFound)
resultsString.Append(matchMode.Value +(Environment.NewLine+nextMath));
this.ResultsTextBox.ForeColor = Color.Black;
this.ResultsTextBox.Text = resultsString.ToString();
}
catch (ArgumentException ex)
{
this.ResultsTextBox.ForeColor = Color.Red;
this.ResultsTextBox.Text = "错误:" + ex.Message;
}
}
private void GroupsButton_Click(object sender, EventArgs e)
{
try
{
RegexOptions selectedRegexOptions = this.GetSelectedRegexOptions();
Regex matchesRegex = new Regex(this.RegexTextBox.Text, selectedRegexOptions);
MatchCollection matchesFound;
matchesFound = matchesRegex.Matches(this.InputTextBox.Text);
String nextMath = "------- 下一个分组 ---------- ";
StringBuilder resultsString = new StringBuilder(64);
GroupCollection matchGroups;
foreach (Match matchMode in matchesFound)
{
matchGroups = matchMode.Groups;
foreach (Group matchGroup in matchGroups)
resultsString.Append("(" + matchGroup.Value + ")");
resultsString.Append(Environment.NewLine + nextMath);
}
this.ResultsTextBox.ForeColor = Color.Black;
this.ResultsTextBox.Text = resultsString.ToString();
}
catch (ArgumentException ex)
{
this.ResultsTextBox.ForeColor = Color.Red;
this.ResultsTextBox.Text = "错误:" + ex.Message;
}
}
//常见正则表达式
private void FamiliarRegex_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
MenuRegex.Show(this,new Point(FamiliarRegex.Location.X, FamiliarRegex.Location.Y + FamiliarRegex.Size.Height));
//MessageBox.Show("sdfsd");
}
private void MenuRegexItem_Click(object sender, EventArgs e)
{
string strRegex = "";
switch (((ToolStripMenuItem)sender).Text)
{
case "整数":
strRegex = @"^((+|-)d)?d*$";
break;
case "浮点数":
strRegex = @"^(?:+|-)?d+(?:.d+)?$";
break;
case "电话号码":
strRegex = @"d{3}-d{8}|d{4}-d{7}";
break;
case "邮政编码":
strRegex = @"[1-9]d{5}(?!d)";
break;
case "Email地址1":
strRegex = @"^(([^><()[]/.,;:@"+'"'+@"
private RegexOptions GetSelectedRegexOptions()
{
RegexOptions selectedRegexOptions = RegexOptions.None;
if (this.IgnoreCaseChkBox.Checked)
selectedRegexOptions |= RegexOptions.IgnoreCase;
if (this.ExplicitCaptureChkBox.Checked)
selectedRegexOptions |= RegexOptions.ExplicitCapture;
if (this.ECMAScriptChkBox.Checked)
selectedRegexOptions |= RegexOptions.ECMAScript;
if (this.IgnoreCaseChkBox.Checked)
selectedRegexOptions |= RegexOptions.IgnoreCase;
if (this.MultiLineChkBox.Checked)
selectedRegexOptions |= RegexOptions.Multiline;
if (this.RightToLeftChkBox.Checked)
selectedRegexOptions |= RegexOptions.RightToLeft;
if (this.SingeLineChkBox.Checked)
selectedRegexOptions |= RegexOptions.Singleline;
return selectedRegexOptions;
}
private void TestRegexButton_Click(object sender, EventArgs e)
{
try
{
RegexOptions selectedRegexOptions = this.GetSelectedRegexOptions();
Regex testRegex = new Regex(this.RegexTextBox.Text, selectedRegexOptions);
if (testRegex.IsMatch(this.InputTextBox.Text))
{
this.ResultsTextBox.ForeColor = Color.Black;
this.ResultsTextBox.Text = "匹配成功";
}
else
{
this.ResultsTextBox.ForeColor = Color.Red;
this.ResultsTextBox.Text = "匹配失败";
}
}
catch (ArgumentException ex)
{
this.ResultsTextBox.ForeColor = Color.Red;
this.ResultsTextBox.Text = "错误:"+ex.Message;
}
}
private void ReplaceButton_Click(object sender, EventArgs e)
{
try
{
RegexOptions selectedRegexOptions = this.GetSelectedRegexOptions();
Regex replaceRegex = new Regex(this.RegexTextBox.Text, selectedRegexOptions);
this.ResultsTextBox.ForeColor = Color.Black;
this.ResultsTextBox.Text = replaceRegex.Replace(this.InputTextBox.Text, this.ReplacementTextBox.Text);
}
catch (ArgumentException ex)
{
this.ResultsTextBox.ForeColor = Color.Red;
this.ResultsTextBox.Text = "错误:" + ex.Message;
}
}
private void SplitBoutton_Click(object sender, EventArgs e)
{
try
{
RegexOptions selectedRegexOptions = this.GetSelectedRegexOptions();
Regex splitRegex = new Regex(this.RegexTextBox.Text, selectedRegexOptions);
String[] splitResults;
splitResults = splitRegex.Split(this.InputTextBox.Text);
StringBuilder resultsString = new StringBuilder(this.InputTextBox.Text.Length);
foreach (String stringElement in splitResults)
resultsString.Append(stringElement + Environment.NewLine);
this.ResultsTextBox.ForeColor = Color.Black;
this.ResultsTextBox.Text = resultsString.ToString();
}
catch (ArgumentException ex)
{
this.ResultsTextBox.ForeColor = Color.Red;
this.ResultsTextBox.Text = "错误:" + ex.Message;
}
}
private void MatchesButton_Click(object sender, EventArgs e)
{
try
{
RegexOptions selectedRegexOptions = this.GetSelectedRegexOptions();
Regex matchesRegex = new Regex(this.RegexTextBox.Text, selectedRegexOptions);
MatchCollection matchesFound;
matchesFound = matchesRegex.Matches(this.InputTextBox.Text);
String nextMath = "------- 下一个匹配项 ---------- ";
StringBuilder resultsString = new StringBuilder(64);
foreach(Match matchMode in matchesFound)
resultsString.Append(matchMode.Value +(Environment.NewLine+nextMath));
this.ResultsTextBox.ForeColor = Color.Black;
this.ResultsTextBox.Text = resultsString.ToString();
}
catch (ArgumentException ex)
{
this.ResultsTextBox.ForeColor = Color.Red;
this.ResultsTextBox.Text = "错误:" + ex.Message;
}
}
private void GroupsButton_Click(object sender, EventArgs e)
{
try
{
RegexOptions selectedRegexOptions = this.GetSelectedRegexOptions();
Regex matchesRegex = new Regex(this.RegexTextBox.Text, selectedRegexOptions);
MatchCollection matchesFound;
matchesFound = matchesRegex.Matches(this.InputTextBox.Text);
String nextMath = "------- 下一个分组 ---------- ";
StringBuilder resultsString = new StringBuilder(64);
GroupCollection matchGroups;
foreach (Match matchMode in matchesFound)
{
matchGroups = matchMode.Groups;
foreach (Group matchGroup in matchGroups)
resultsString.Append("(" + matchGroup.Value + ")");
resultsString.Append(Environment.NewLine + nextMath);
}
this.ResultsTextBox.ForeColor = Color.Black;
this.ResultsTextBox.Text = resultsString.ToString();
}
catch (ArgumentException ex)
{
this.ResultsTextBox.ForeColor = Color.Red;
this.ResultsTextBox.Text = "错误:" + ex.Message;
}
}
//常见正则表达式
private void FamiliarRegex_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
MenuRegex.Show(this,new Point(FamiliarRegex.Location.X, FamiliarRegex.Location.Y + FamiliarRegex.Size.Height));
//MessageBox.Show("sdfsd");
}
private void MenuRegexItem_Click(object sender, EventArgs e)
{
string strRegex = "";
switch (((ToolStripMenuItem)sender).Text)
{
case "整数":
strRegex = @"^((+|-)d)?d*$";
break;
case "浮点数":
strRegex = @"^(?:+|-)?d+(?:.d+)?$";
break;
case "电话号码":
strRegex = @"d{3}-d{8}|d{4}-d{7}";
break;
case "邮政编码":
strRegex = @"[1-9]d{5}(?!d)";
break;
case "Email地址1":
strRegex = @"^(([^><()[]/.,;:@"+'"'+@"
下载地址:http://download.csdn.net/source/162304
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1535529
- 也谈正则表达式
- 也谈正则表达式
- 也谈正则表达式
- 也谈正则表达式
- Matlab也有正则表达式grexp
- C++中的也能使用正则表达式
- C++中的也能使用正则表达式
- 关于正则表达式,好像java代码有正则表达式,sql也有正则表达式
- 发现正则表达式的缺陷!也欢迎大家来讨论
- 使用RegexKitLite使iphone也可以使用正则表达式
- 不错的手机正则表达式,解释的也还不错.
- 正则表达式匹配也可以简单快速(上:原理部分)
- 正则表达式匹配也可以简单快速(下:实现部分)
- iOS在4.0里也可以用正则表达式了
- 原来C语言也有正则表达式的库
- 正则表达式用成这样也算是没sei了
- 正则表达式验证整数,小数点后为0也可以
- 也谈如何在C语言中巧用正则表达式,并且如何将其转化成C++的形式
- Windows Vista 中文版的兼容性
- Windows Vista不兼容VS2005(需打补丁)
- WinCE5.0平台下的Moxa DA66x设备应用开发心得
- .net精简框架集下的ini文件读取(C#)
- 我学javascrip,刚刚学啊。希望大家多教点东西给我啊!!
- 也谈正则表达式
- JSF编程的一些笔记(摘抄)
- 初涉龙芯I/龙芯II
- 【C#】串口操作实用类
- 2007微软MVP获得连任
- DataRabbit 轻量的数据访问框架(00) -- 序
- CSDN技术大会
- .Net 平台SerialPort类内部实现探秘
- 简单生活.