其实很早就知道了正则表达式,在集成VBScript脚本的时候,就看到了该功能,不过那时觉得很难,觉得也派不上什么用场,所以也没有过多关注。
最近看了<personname productid="孟岩" w:st="on">孟岩</personname>老师的关于正则表达式讲解,有一种学习正则表达式的冲动,适时我们开发的项目中需要嵌入Python脚本功能,需要一个脚本编辑器,关键字变色等等相关功能用正则表达式实现相对容易的多。
前段时间买了本红皮书《C#字符串和正则表达式参考书》(这真是一本好书,想学习正则表达式的可以参考一下),花了几天的时间把该书看了一遍,正则表达式的用法基本上也弄清楚了,并且对字符串相关的知识也越来越感兴趣了。
对正则表达式的具体规则和使用,实没有什么可说的,网上的文章多的很,都说的比我的好。这是我学习正则表达式做的一个简单正则表达式测试工具,其实大部分代码就是上面书的一个示例(不知道为什么,上网竟没有找到该书的示例源码),又上网查了一些资料,把一些常见的正则表达式也嵌入了进去,方便了正则表达式的应用(以后有时间做一个比较理想的正则表达式工具)。
这是程序的截图:
相关源码:
//获取正则表达式的匹配参数
privateRegexOptionsGetSelectedRegexOptions()
{
RegexOptionsselectedRegexOptions=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;
returnselectedRegexOptions;
}
privatevoidTestRegexButton_Click(objectsender,EventArgse)
{
try
{
RegexOptionsselectedRegexOptions=this.GetSelectedRegexOptions();
RegextestRegex=newRegex(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(ArgumentExceptionex)
{
this.ResultsTextBox.ForeColor=Color.Red;
this.ResultsTextBox.Text="错误:"+ex.Message;
}
}
privatevoidReplaceButton_Click(objectsender,EventArgse)
{
try
{
RegexOptionsselectedRegexOptions=this.GetSelectedRegexOptions();
RegexreplaceRegex=newRegex(this.RegexTextBox.Text,selectedRegexOptions);
this.ResultsTextBox.ForeColor=Color.Black;
this.ResultsTextBox.Text=replaceRegex.Replace(this.InputTextBox.Text,this.ReplacementTextBox.Text);
}
catch(ArgumentExceptionex)
{
this.ResultsTextBox.ForeColor=Color.Red;
this.ResultsTextBox.Text="错误:"+ex.Message;
}
}
privatevoidSplitBoutton_Click(objectsender,EventArgse)
{
try
{
RegexOptionsselectedRegexOptions=this.GetSelectedRegexOptions();
RegexsplitRegex=newRegex(this.RegexTextBox.Text,selectedRegexOptions);
String[]splitResults;
splitResults=splitRegex.Split(this.InputTextBox.Text);
StringBuilderresultsString=newStringBuilder(this.InputTextBox.Text.Length);
foreach(StringstringElementinsplitResults)
resultsString.Append(stringElement+Environment.NewLine);
this.ResultsTextBox.ForeColor=Color.Black;
this.ResultsTextBox.Text=resultsString.ToString();
}
catch(ArgumentExceptionex)
{
this.ResultsTextBox.ForeColor=Color.Red;
this.ResultsTextBox.Text="错误:"+ex.Message;
}
}
privatevoidMatchesButton_Click(objectsender,EventArgse)
{
try
{
RegexOptionsselectedRegexOptions=this.GetSelectedRegexOptions();
RegexmatchesRegex=newRegex(this.RegexTextBox.Text,selectedRegexOptions);
MatchCollectionmatchesFound;
matchesFound=matchesRegex.Matches(this.InputTextBox.Text);
StringnextMath="-------下一个匹配项---------- ";
StringBuilderresultsString=newStringBuilder(64);
foreach(MatchmatchModeinmatchesFound)
resultsString.Append(matchMode.Value+(Environment.NewLine+nextMath));
this.ResultsTextBox.ForeColor=Color.Black;
this.ResultsTextBox.Text=resultsString.ToString();
}
catch(ArgumentExceptionex)
{
this.ResultsTextBox.ForeColor=Color.Red;
this.ResultsTextBox.Text="错误:"+ex.Message;
}
}
privatevoidGroupsButton_Click(objectsender,EventArgse)
{
try
{
RegexOptionsselectedRegexOptions=this.GetSelectedRegexOptions();
RegexmatchesRegex=newRegex(this.RegexTextBox.Text,selectedRegexOptions);
MatchCollectionmatchesFound;
matchesFound=matchesRegex.Matches(this.InputTextBox.Text);
StringnextMath="-------下一个分组---------- ";
StringBuilderresultsString=newStringBuilder(64);
GroupCollectionmatchGroups;
foreach(MatchmatchModeinmatchesFound)
{
matchGroups=matchMode.Groups;
foreach(GroupmatchGroupinmatchGroups)
resultsString.Append("("+matchGroup.Value+")");
resultsString.Append(Environment.NewLine+nextMath);
}
this.ResultsTextBox.ForeColor=Color.Black;
this.ResultsTextBox.Text=resultsString.ToString();
}
catch(ArgumentExceptionex)
{
this.ResultsTextBox.ForeColor=Color.Red;
this.ResultsTextBox.Text="错误:"+ex.Message;
}
}
//常见正则表达式
privatevoidFamiliarRegex_LinkClicked(objectsender,LinkLabelLinkClickedEventArgse)
{
MenuRegex.Show(this,newPoint(FamiliarRegex.Location.X,FamiliarRegex.Location.Y+FamiliarRegex.Size.Height));
//MessageBox.Show("sdfsd");
}
privatevoidMenuRegexItem_Click(objectsender,EventArgse)
{
stringstrRegex="";
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
<iframe align="center" marginwidth="0" marginheight="0" src="http://www.zealware.com/csdnblog.html" frameborder="0" width="728" scrolling="no" height="90"></iframe>
分享到:
相关推荐
谈正则表达式在InSight PCB系统中的应用.pdf
浅谈正则表达式在文本排版及校对中的应用 可以在word等中使用
正则表达式经验谈 总结正则表达式常用的方法及出现的问题
浅谈正则表达式实例入门共9页.pdf.zip
正则表达式是具有贪婪性的,我们从下面这例谈起: 已知str="uid=100(guest) gid=100(others) groups=10(users),11(floppy)" 现在想要得到这个字符串中的第一个括号内的值,即guest该怎么办?假设$str的括号外的内容...
以上这篇利用Python正则表达式过滤敏感词的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持软件开发网。 您可能感兴趣的文章:Python 实现王者荣耀中的敏感词过滤示例python 实现...
10.19_小谈JSON、JSON对象与JSON字符串|正则表达式与JSON|Python3.8入门_&_进阶_&_原生爬虫实战
一个很好使的验证正则表达式的测试小工具,还有一个华为内部正则表达式pdf文档资料。该工具内部已集成了一些常用的正则表达式,也可以自己根据需要写正则表达式,然后测试。非常棒,小巧玲珑的小测试工具
文档讲述了正则表达式的基本语法、使用技巧,是本人通过测试的学习总结,非常适合初学者,因为我也是作为初学者来研究的
正则表达式格式 PHP变成的利器,经过一段时间的学习,浅谈一下自己的心得!!!!
很长时间没看 正则表达式了,碰巧今天用到,温故知新了一把 看书学习吧 50% 的举一反三练习中的原创。 一 javascript正则表达式的基本知识 1 javascript 正则对象创建 和用法 声明javascript 正则表达式 代码如下: ...
一个正则表达式要如何书写才能同时匹配这两个数字呢?简单的字符表达式当然无法完成了,这个时候我们就可以定义一个字符集合(字符类)来进行匹配。这就是分组匹配了
之前我对正则表达式也是略有研究,想尝试一下。其实我对正则表达式的学习基本完全来源于犀牛书的第10章,真正看懂这一章,我觉得操作正则表达式应该不在话下。 我的答案 先给出我的答案吧: ‘abbccddd’.match(/(\...
本文介绍了正则表达式的一些学习内容,以及在Javascript、PHP下如何使用正则表达式
下面小编就为大家带来一篇JavaScript正则表达式-非捕获性分组。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
前言 Python文件默认的编码格式是ascii ,无法识别汉字,因为ascii码中没有中文。 所以py文件中要写中文...注2:python3.x的源码文件默认使用utf-8编码,可以解析中文,开头不指定也行,但为了规范和避免一些意想不到
比如我们要匹配以字母”a”开头字母”b”结尾的字符串,但是需要匹配的字符串在”a”后面含有很多个”b”,比如”a bbbbbbbbbbbbbbbbb”,那正则表达式是会匹配第一个”b”还是最后一个”b”呢?如果你使用了贪婪...