比较不错的C#中的常用的正则表达式
程序员文章站
2022-07-02 20:38:10
为了能够更好地理解如何在c#环境中使用正则表达式,我写出一些对你来说可能有用的正则表达式,这些表达式在其他的环境中都被使用过,希望能够对你有所帮助。 罗马数字...
为了能够更好地理解如何在c#环境中使用正则表达式,我写出一些对你来说可能有用的正则表达式,这些表达式在其他的环境中都被使用过,希望能够对你有所帮助。
罗马数字
string p1 = "^m*(d?c{0,3}|c[dm])" + "(l?x{0,3}|x[lc])(v?i{0,3}|i[vx])$";
string t1 = "vii";
match m1 = regex.match(t1, p1);
交换前二个单词
string t2 = "the quick brown fox";
string p2 = @"(\s+)(\s+)(\s+)";
regex x2 = new regex(p2);
string r2 = x2.replace(t2, "$3$2$1", 1);
关健字=值
string t3 = "myval = 3";
string p3 = @"(\w+)\s*=\s*(.*)\s*$";
match m3 = regex.match(t3, p3);
实现每行80个字符
string t4 = "********************"
+ "******************************"
+ "******************************";
string p4 = ".{80,}";
match m4 = regex.match(t4, p4);
月/日/年 小时:分:秒的时间格式
string t5 = "01/01/01 16:10:01";
string p5 = @"(\d+)/(\d+)/(\d+) (\d+):(\d+):(\d+)";
match m5 = regex.match(t5, p5);
改变目录(仅适用于windows平台)
string t6 = @"c:\documents and settings\user1\desktop\";
string r6 = regex.replace(t6,@"\\user1\\", @"\\user2\\");
扩展16位转义符
string t7 = "%41"; // capital a
string p7 = "%([0-9a-fa-f][0-9a-fa-f])";
string r7 = regex.replace(t7, p7, hexconvert);
删除c语言中的注释(有待完善)
string t8 = @"
/*
* 传统风格的注释
*/
";
string p8 = @"
/\* # 匹配注释开始的定界符
.*? # 匹配注释
\*/ # 匹配注释结束定界符
";
string r8 = regex.replace(t8, p8, "", "xs");
删除字符串中开始和结束处的空格
string t9a = " leading";
string p9a = @"^\s+";
string r9a = regex.replace(t9a, p9a, "");
string t9b = "trailing ";
string p9b = @"\s+$";
string r9b = regex.replace(t9b, p9b, "");
在字符\后添加字符n,使之成为真正的新行
string t10 = @"\ntest\n";
string r10 = regex.replace(t10, @"\\n", "\n");
转换ip地址
string t11 = "55.54.53.52";
string p11 = "^" +
@"([01]?\d\d|2[0-4]\d|25[0-5])\." +
@"([01]?\d\d|2[0-4]\d|25[0-5])\." +
@"([01]?\d\d|2[0-4]\d|25[0-5])\." +
@"([01]?\d\d|2[0-4]\d|25[0-5])" +
"$";
match m11 = regex.match(t11, p11);
删除文件名包含的路径
string t12 = @"c:\file.txt";
string p12 = @"^.*\\";
string r12 = regex.replace(t12, p12, "");
联接多行字符串中的行
string t13 = @"this is
a split line";
string p13 = @"\s*\r?\n\s*";
string r13 = regex.replace(t13, p13, " ");
提取字符串中的所有数字
string t14 = @"
test 1
test 2.3
test 47
";
string p14 = @"(\d+\.?\d*|\.\d+)";
matchcollection mc14 = regex.matches(t14, p14);
找出所有的大写字母
string t15 = "this is a test of all caps";
string p15 = @"(\b[^\wa-z0-9_]+\b)";
matchcollection mc15 = regex.matches(t15, p15);
找出小写的单词
string t16 = "this is a test of lowercase";
string p16 = @"(\b[^\wa-z0-9_]+\b)";
matchcollection mc16 = regex.matches(t16, p16);
找出第一个字母为大写的单词
string t17 = "this is a test of initial caps";
string p17 = @"(\b[^\wa-z0-9_][^\wa-z0-9_]*\b)";
matchcollection mc17 = regex.matches(t17, p17);
找出简单的html语言中的链接
string t18 = @"
<html>
<a href=""first.htm"">first tag text</a>
<a href=""next.htm"">next tag text</a>
</html>
";
string p18 = @"<a[^>]*?href\s*=\s*[""']?" + @"([^'"" >]+?)[ '""]?>";
matchcollection mc18 = regex.matches(t18, p18, "si");
罗马数字
string p1 = "^m*(d?c{0,3}|c[dm])" + "(l?x{0,3}|x[lc])(v?i{0,3}|i[vx])$";
string t1 = "vii";
match m1 = regex.match(t1, p1);
交换前二个单词
string t2 = "the quick brown fox";
string p2 = @"(\s+)(\s+)(\s+)";
regex x2 = new regex(p2);
string r2 = x2.replace(t2, "$3$2$1", 1);
关健字=值
string t3 = "myval = 3";
string p3 = @"(\w+)\s*=\s*(.*)\s*$";
match m3 = regex.match(t3, p3);
实现每行80个字符
string t4 = "********************"
+ "******************************"
+ "******************************";
string p4 = ".{80,}";
match m4 = regex.match(t4, p4);
月/日/年 小时:分:秒的时间格式
string t5 = "01/01/01 16:10:01";
string p5 = @"(\d+)/(\d+)/(\d+) (\d+):(\d+):(\d+)";
match m5 = regex.match(t5, p5);
改变目录(仅适用于windows平台)
string t6 = @"c:\documents and settings\user1\desktop\";
string r6 = regex.replace(t6,@"\\user1\\", @"\\user2\\");
扩展16位转义符
string t7 = "%41"; // capital a
string p7 = "%([0-9a-fa-f][0-9a-fa-f])";
string r7 = regex.replace(t7, p7, hexconvert);
删除c语言中的注释(有待完善)
string t8 = @"
/*
* 传统风格的注释
*/
";
string p8 = @"
/\* # 匹配注释开始的定界符
.*? # 匹配注释
\*/ # 匹配注释结束定界符
";
string r8 = regex.replace(t8, p8, "", "xs");
删除字符串中开始和结束处的空格
string t9a = " leading";
string p9a = @"^\s+";
string r9a = regex.replace(t9a, p9a, "");
string t9b = "trailing ";
string p9b = @"\s+$";
string r9b = regex.replace(t9b, p9b, "");
在字符\后添加字符n,使之成为真正的新行
string t10 = @"\ntest\n";
string r10 = regex.replace(t10, @"\\n", "\n");
转换ip地址
string t11 = "55.54.53.52";
string p11 = "^" +
@"([01]?\d\d|2[0-4]\d|25[0-5])\." +
@"([01]?\d\d|2[0-4]\d|25[0-5])\." +
@"([01]?\d\d|2[0-4]\d|25[0-5])\." +
@"([01]?\d\d|2[0-4]\d|25[0-5])" +
"$";
match m11 = regex.match(t11, p11);
删除文件名包含的路径
string t12 = @"c:\file.txt";
string p12 = @"^.*\\";
string r12 = regex.replace(t12, p12, "");
联接多行字符串中的行
string t13 = @"this is
a split line";
string p13 = @"\s*\r?\n\s*";
string r13 = regex.replace(t13, p13, " ");
提取字符串中的所有数字
string t14 = @"
test 1
test 2.3
test 47
";
string p14 = @"(\d+\.?\d*|\.\d+)";
matchcollection mc14 = regex.matches(t14, p14);
找出所有的大写字母
string t15 = "this is a test of all caps";
string p15 = @"(\b[^\wa-z0-9_]+\b)";
matchcollection mc15 = regex.matches(t15, p15);
找出小写的单词
string t16 = "this is a test of lowercase";
string p16 = @"(\b[^\wa-z0-9_]+\b)";
matchcollection mc16 = regex.matches(t16, p16);
找出第一个字母为大写的单词
string t17 = "this is a test of initial caps";
string p17 = @"(\b[^\wa-z0-9_][^\wa-z0-9_]*\b)";
matchcollection mc17 = regex.matches(t17, p17);
找出简单的html语言中的链接
string t18 = @"
<html>
<a href=""first.htm"">first tag text</a>
<a href=""next.htm"">next tag text</a>
</html>
";
string p18 = @"<a[^>]*?href\s*=\s*[""']?" + @"([^'"" >]+?)[ '""]?>";
matchcollection mc18 = regex.matches(t18, p18, "si");