Regexp

Remember that shell meta-characters are expanded before the shell passes the arguments to the program. To prevent this expansion, the special characters in a regular expression must be quoted when passed as an option from the shell.


Anchors        are used to specify the position of the pattern in relation to a line of text.

Character Sets match one or more characters in a single position.

Modifiers      specify how many times the previous character set is repeated.

There are also two types of regular expressions: the"Basic" regular expression, and the"extended" regular expression.

The Anchor Characters: ^ and $

如果^不再第一个位置$不再最后一个位置的话,他们都将表示普通的字符


Matching a character with a character set

最简单的字符集就是指单个的字符,比如正则表达式"the"包含3个字符"t" "h" "e",你可以在字符串的前面加入^进行起始位置的匹配

grep '^From: ' /usr/spool/mail/$USER   匹配那些以"From: "开头的字符串


Match any character with .

^.$  匹配一个单一的字符


Exceptions in a character set

You can easily search for all characters except those in square brackets by putting a"^" as the first character after the "[". To match all characters except vowels use"[^aeiou]".

Like the anchors in places that can't be considered an anchor, the characters"]" and"-" do not have a special meaning if they directly follow"[". Here are some examples:


Repeating character sets with *

^#*  is useless, as it matches any number of "#'s" at the beginning of the line, includingzero. Therefore this will match every line, because every line starts withzero or more "#'s".

If you need to match one or more, just repeat the character set.That is, "[0-9]*" matches zero or more numbers, and"[0-9][0-9]*" matches one or more numbers.


Matching a specific number of sets with \{ and \}

通过字符*,我们可以有最少的重复次数,而不能设定上限,现在在\{" 和"\}"之间加入两个数字就可以实现控制重复次数的上下限。

正常情况下,反斜杠会取消字符的特殊含义,比如\.和\*。但是当反斜杠放到"<," ">," "{," "}," "(," "),"或数字之前就会赋予他们特殊的含义。

你可以任意选择0-255之间的数,如果省略掉第二个数字,就没有上限,如果省略了第二个数字和逗号,必须重复所限定的次数。

注意点:modifier"*" 和"\{1,5\}"必须跟在跟在一个字符集(包括单个字符)的后面才表示重复出现的功能,如果在开始位置的话,就不再具有指定重复次数的功能。



Matching words with \< and \>

我们前面的匹配用“the”,但是在我们进行匹配的时候other也会被匹配到。而现在\<和\>就类似^跟$,因为他们两个不占字符,他们用于指示单词的边界,

\<[tT]he\> t 前面只能时换行符,或除数字,字母和下划线之外的所有字符,e后面同样只能是行的结尾,或者除数字,字母和下划线之外的所有字符


Back_references - Remembering patterns with \(, \) and \1

当你希望可以匹配到相等的结构时,这个时候你就需要记住前面匹配的结果。然后检测相同的结构是否再出现;你可以用\(和\)来标注这个结构,并且通过\后跟数字来实现对储存结构的调用,

\([a-z]\)\1     查找连续两个相同字符的结构

\([a-z]\)\([a-z]\)[a-z]\2\1 关于中间对称的字符串结构,比如“radar"


\(...\|...\) show alteration in Emacs

(...|...)    show alteration in egrep

也许你看到Wiki 中对{}和()都是没有斜杠的,而经过我的验证在Emacs的相应功能的实现都是需要斜杠的,比如下面的例子

\([a-z]\).*\1  在emacs中找出连续两个相同的字符
\(This\|If\)   在Emacs中找出This或If


下面是一个复杂的正则表达式,存储在“sentence-end”,Emacs 将其用于识别句子的结束以及后面的任何空白。其中以 Lisp 语法区分了空白符和制表符。在Lisp 语法中,串常量用双引号括起来。“\"”表示双引号是表达式的一部分,“\\”表示反斜扛是表达式的一部分,“\t”表示制表符,“\n”表示换行。
 "[.?!][]\”‘)]*\\($\\| $\\|\t\\|  \\)[ \t\n]*”
其中包含四个连续的部分:匹配句号(“.”)、“?”或“!”的字符集;匹配右方括号、右(单/双)引号的字符集的任意次重复的部分;在“反斜线括号”部分中,匹配行尾、行尾空白、制表符或两个空格的可选集合;以及一个任意次匹配空白的字符集。

验证数字的正则表达式集 
验证数字:^[0-9]*$
验证n位的数字:^\d{n}$
验证至少n位数字:^\d{n,}$
验证m-n位的数字:^\d{m,n}$
验证零和非零开头的数字:^(0|[1-9][0-9]*)$
验证有两位小数的正实数:^[0-9]+(.[0-9]{2})?$
验证有1-3位小数的正实数:^[0-9]+(.[0-9]{1,3})?$
验证非零的正整数:^\+?[1-9][0-9]*$
验证非零的负整数:^\-[1-9][0-9]*$
验证非负整数(正整数 + 0)  ^\d+$
验证非正整数(负整数 + 0)  ^((-\d+)|(0+))$
验证长度为3的字符:^.{3}$
验证由26个英文字母组成的字符串:^[A-Za-z]+$
验证由26个大写英文字母组成的字符串:^[A-Z]+$
验证由26个小写英文字母组成的字符串:^[a-z]+$
验证由数字和26个英文字母组成的字符串:^[A-Za-z0-9]+$
验证由数字、26个英文字母或者下划线组成的字符串:^\w+$
验证用户密码:^[a-zA-Z]\w{5,17}$ 正确格式为:以字母开头,长度在6-18之间,只能包含字符、数字和下划线。
验证是否含有 ^%&',;=?$\" 等字符:[^%&',;=?$\x22]+
验证汉字:^[\u4e00-\u9fa5],{0,}$
验证Email地址:^\w+[-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$
验证InternetURL:^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$ ;^[a-zA-z]+://(w+(-w+)*)(.(w+(-w+)*))*(?S*)?$
验证电话号码:^(\(\d{3,4}\)|\d{3,4}-)?\d{7,8}$:--正确格式为:XXXX-XXXXXXX,XXXX-XXXXXXXX,XXX-XXXXXXX,XXX-XXXXXXXX,XXXXXXX,XXXXXXXX。
验证身份证号(15位或18位数字):^\d{15}|\d{}18$
验证一年的12个月:^(0?[1-9]|1[0-2])$ 正确格式为:“01”-“09”和“1”“12”
验证一个月的31天:^((0?[1-9])|((1|2)[0-9])|30|31)$    正确格式为:01、09和1、31。
整数:^-?\d+$
非负浮点数(正浮点数 + 0):^\d+(\.\d+)?$
正浮点数   ^(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*))$
非正浮点数(负浮点数 + 0) ^((-\d+(\.\d+)?)|(0+(\.0+)?))$
负浮点数  ^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$
浮点数  ^(-?\d+)(\.\d+)?



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值