手机
当前位置:查字典教程网 >编程开发 >正则表达式 >比较不错的C#中的常用的正则表达式
比较不错的C#中的常用的正则表达式
摘要:为了能够更好地理解如何在C#环境中使用正则表达式,我写出一些对你来说可能有用的正则表达式,这些表达式在其他的环境中都被使用过,希望能够对你有...

为了能够更好地理解如何在C#环境中使用正则表达式,我写出一些对你来说可能有用的正则表达式,这些表达式在其他的环境中都被使用过,希望能够对你有所帮助。

罗马数字

stringp1="^m*(d?c{0,3}|c[dm])"+"(l?x{0,3}|x[lc])(v?i{0,3}|i[vx])$";

stringt1="vii";

Matchm1=Regex.Match(t1,p1);

交换前二个单词

stringt2="thequickbrownfox";

stringp2=@"(S+)(s+)(S+)";

Regexx2=newRegex(p2);

stringr2=x2.Replace(t2,"$3$2$1",1);

关健字=值

stringt3="myval=3";

stringp3=@"(w+)s*=s*(.*)s*$";

Matchm3=Regex.Match(t3,p3);

实现每行80个字符

stringt4="********************"

+"******************************"

+"******************************";

stringp4=".{80,}";

Matchm4=Regex.Match(t4,p4);

月/日/年小时:分:秒的时间格式

stringt5="01/01/0116:10:01";

stringp5=@"(d+)/(d+)/(d+)(d+):(d+):(d+)";

Matchm5=Regex.Match(t5,p5);

改变目录(仅适用于Windows平台)

stringt6=@"C:DocumentsandSettingsuser1Desktop";

stringr6=Regex.Replace(t6,@"user1",@"user2");

扩展16位转义符

stringt7="%41";//capitalA

stringp7="%([0-9A-Fa-f][0-9A-Fa-f])";

stringr7=Regex.Replace(t7,p7,HexConvert);

删除C语言中的注释(有待完善)

stringt8=@"

/*

*传统风格的注释

*/

";

stringp8=@"

/*#匹配注释开始的定界符

.*?#匹配注释

*/#匹配注释结束定界符

";

stringr8=Regex.Replace(t8,p8,"","xs");

删除字符串中开始和结束处的空格

stringt9a="leading";

stringp9a=@"^s+";

stringr9a=Regex.Replace(t9a,p9a,"");

stringt9b="trailing";

stringp9b=@"s+$";

stringr9b=Regex.Replace(t9b,p9b,"");

在字符后添加字符n,使之成为真正的新行

stringt10=@"ntestn";

stringr10=Regex.Replace(t10,@"n","n");

转换IP地址

stringt11="55.54.53.52";

stringp11="^"+

@"([01]?dd|2[0-4]d|25[0-5])."+

@"([01]?dd|2[0-4]d|25[0-5])."+

@"([01]?dd|2[0-4]d|25[0-5])."+

@"([01]?dd|2[0-4]d|25[0-5])"+

"$";

Matchm11=Regex.Match(t11,p11);

删除文件名包含的路径

stringt12=@"c:file.txt";

stringp12=@"^.*";

stringr12=Regex.Replace(t12,p12,"");

联接多行字符串中的行

stringt13=@"thisis

asplitline";

stringp13=@"s*r?ns*";

stringr13=Regex.Replace(t13,p13,"");

提取字符串中的所有数字

stringt14=@"

test1

test2.3

test47

";

stringp14=@"(d+.?d*|.d+)";

MatchCollectionmc14=Regex.Matches(t14,p14);

找出所有的大写字母

stringt15="ThisISaTestOFALLCaps";

stringp15=@"(b[^Wa-z0-9_]+b)";

MatchCollectionmc15=Regex.Matches(t15,p15);

找出小写的单词

stringt16="ThisisATestoflowercase";

stringp16=@"(b[^WA-Z0-9_]+b)";

MatchCollectionmc16=Regex.Matches(t16,p16);

找出第一个字母为大写的单词

stringt17="ThisisATestofInitialCaps";

stringp17=@"(b[^Wa-z0-9_][^WA-Z0-9_]*b)";

MatchCollectionmc17=Regex.Matches(t17,p17);

找出简单的HTML语言中的链接

stringt18=@"

<html>

<ahref=""first.htm"">firsttagtext</a>

<ahref=""next.htm"">nexttagtext</a>

</html>

";

stringp18=@"<A[^>]*?HREFs*=s*[""']?"+@"([^'"">]+?)['""]?>";

MatchCollectionmc18=Regex.Matches(t18,p18,"si");

【比较不错的C#中的常用的正则表达式】相关文章:

一个关于正则表达式的问题

正则表达式 应用四则

php利用正则表达式取出图片的URL

php中正则表达式中的特殊符号

好东西,老外用正则表达式写的HTML分离函数

js:日期正则表达式及检测

正则表达式验证

用正则表达式来表示中文

C#中的正则表达式 学习资料

php半小时精通正则表达式

精品推荐
分类导航