首页 > 学院 > 开发设计 > 正文

正则表达式入门及备忘

2019-11-17 02:16:03
字体:
来源:转载
供稿:网友

正则表达式入门及备忘

2015-07-14 20:39 by Mike.Jiang, ... 阅读, ... 评论, 收藏, 编辑

概述

正则表达式,主要是用符号描述了一类特定的文本(模式)。而正则表达式引擎则负责在给定的字符串中,查找到这一特定的文本。

本文主要是列出常用的正则表达式符号,加以归类说明。本文仅仅是快速理解了正则表达式相关元字符,作一个备忘,供以后理解更复杂表达式的参考,以后关于正则表达式的相关内容会持续更新本文。示例语言用C#

概述

普通字符

字符集合

速记的字符集合

指定重复次数的字符

匹配位置字符

分支替换字符

匹配特殊字符

组,反向引用,非捕获组

贪婪与非贪婪

回溯与非回溯

正向预搜索、反向预搜索

最后

1 普通字符

最简单的一种文本描述,就是直接给出要匹配内容。 如要在”Generic specialization, the decorator pattern, chains of responsibilities, and extensible software.” 找到pattern,那么正则式就直接是”heels”即可

            string input = "Generic specialization, the decorator pattern, chains of responsibilities, and extensible software.";            Regex reg = new Regex("pattern", RegexOptions.IgnoreCase);            Console.WriteLine(reg.Matches(input).Count);  //output 1
View Code

2 字符集合

将字符放在中括号中,即为字符集合。通过字符集合告诉正则式引擎从字符集合中的字符,仅匹配出一个字符。

字符匹配的字符示例
[...]匹配括号中的任一字符[abc]可以匹配单个字符a,b或c,但不能匹配其它字符
[^...]匹配非括号中的任一字符[^abc]可以匹配任一个除a,b,c的一个字符,如d,e,f

比如单词灰色gray(英)和grey(美),在一段文本中匹配出gray或grey,那么通过正则式gr[ae]y 就可以了;又比如要在一段文本中找到me和my,正则式是m[ey]

我们还可以在字符集合中使用连字号 – 来表示一个范围,比如 [0-9] 表示匹配一个0到9数字;[a-zA-Z] 表示匹配英文字母;[0-9 a-zA-Z]表示匹配一个0到9的数字或英文字母

            string input = "The color of shirt is gray and color of shoes is grey too.";            Regex reg = new Regex("gr[ae]y", RegexOptions.IgnoreCase);            Console.WriteLine(reg.Matches(input).Count);  //output 2            var matchs = reg.Matches(input);            foreach (Match match in matchs)            {                Console.WriteLine(match.Value);//output gray  grey            }
View Code

3 速记的字符集合

我们常常要匹配一个数字,一个字母,一个空白符,虽然可以用普通的字符类来表示,但不够方便,所以正则式提示了一些常用的字符集合的速记符

字符匹配的字符示例
/d从0到9的任何一个数字/d/d 可以匹配72,但不能匹配me或7a
/D非数字符/D/D 可以匹配me,但不能匹配7a或72
/w任一个单词字符,如A-Z, a-z, 0-9和下划线字符/w/w/w/w可以匹配aB_2,但不能匹配ab_@
/W非单词字符/W 可以匹配@,但不能匹配a
/s任一空白字符,包括了制表符,换行符,回车符,换页符和垂直制表符匹配所有传统的空白字符
/S任一非空白字符/S 可以匹配任一非空白字符,如~!@#&
.任一字符匹配任一字符,换行符除外
            string input = "1024 hello world&%";            Regex reg1 = new Regex(@"/d/d/d/d");            if (reg1.IsMatch(input))             {                Console.WriteLine(reg1.Match(input).Value);//output 1024            }            Regex reg2 = new Regex(@"/W/W");            if (reg2.IsMatch(input))            {                Console.WriteLine(reg2.Match(input).Value);//output &%            }
View Code

4 指定重复次数的字符

指定重复匹配前面的字符多少次:匹配重复的次数,不匹配内容。比如说,要在一系列电话号码中找到以158开始的11位手机号,如果我们没有学过下面的内容,正则表达式为158/d/d/d/d/d/d/d/d;但如果我们学习了下面的知识,则正则表达式为158/d{8}

字符匹配的字符示例
{n}匹配前面字符n次x{2},可以匹配xx,但不能匹配xxx
{n,}匹配前面字符n次或更多x{2,}可以2个或更多的x,比如可以匹配xx,xxx,xxxx,xxxxxx
{n,m}匹配前面字符最少n次,最多m次。如果n为0,则可以不指定x{2,4},匹配了xx,xxx,xxxx,但不能匹配x,xxxxx
?匹配前面的字符0次或1次,相当于{0,1}x? 匹配x或空
+匹配前面的字符1次或多次, 相当于{1,}x+ 匹配x,xx,或xxx
*匹配前面的字符0次或多次x* 匹配0个或多个x
            string input = "my phone number is 15861327445, please call me sometime.";            Regex reg1 = new Regex(@"158/d{8}");//匹配以158为开头的11位手机号            if (reg1.IsMatch(input))            {                Console.WriteLine(reg1.Match(input).Value);//output 15861327445            }            string input2 = "November is the 11 month of the year, you can use Nov for short.";            Regex reg2 = new Regex(@"Nov(ember)?");//匹配Nov 或者November            var matchs = reg2.Matches(input2);            foreach (Match match in matchs)            {                Console.WriteLine(match.Value);//output November  Nov            }            string input3 = "1000, 100, 2003, 9999,10000";            Regex reg3 = new Regex(@"/b[1-9]/d{3}/b");//匹配1000到9999的数字            var matchs3 = reg3.Matches(input3);            foreach (Match match in matchs3)            {                Console.WriteLine(match.Value);//output
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表