正则表达式快速入门教程

摘要:看到一本书写的正则表达式教程非常好,特地整理出来,本教程结合linux的grep命令,可以让大家迅速掌握正则表达式。正则在nginx配置和linux命令中应用非常广泛。这个正则教程尽量写的简单,肯定可以看懂,如果碰到一个很繁琐的正则表达式,只要耐心分析肯定可以看懂,因为正则表达式都是一段一段的,不像复杂抽象的程序逻辑。

grep是常用的linux命令,用于字符串数据的对比,将符合条件的字符串打印出来。

1

grep '搜寻字符串' filename

一个栗子:

12

grep 'root' /etc/passwdroot:x:0:0:root:/root:/bin/bash

为了显示突出显示效果也就是高亮效果,可以定义grep别名:

1

grep='grep --color=auto'

范例文件r.txt

在linux可以通过下列命令获取:

123456789101112131415161718192021222324

wget http://linux.vbird.org/linux_basic/0330regularex/regular_express.txtmv regular_express.txt r.txtcat r.txt"Open Source" is a good mechanism to develop programs.apple is my favorite food.Football game is not use feet only.this dress doesn't fit me.However, this dress is about $ 3183 dollars.GNU is free air not free beer.Her hair is very beauty.I can't finish the test.Oh! The soup taste good.motorcycle is cheap than car.This window is clear.the symbol '*' is represented as start.Oh! My god!The gd software is a library for drafting programs.You are the best is mean you are the no. 1.The world <Happy> is the same with "glad".I like dog.google is the best tools for search keyword.goooooogle yes!go! go! Let's go.# I am VBird

这文件一共22行,最后一行是空白行。
基础正则表达式的练习
例一:

1234567

grep -n 'the' r.txt8:I can't finish the test.12:the symbol '*' is represented as start.15:You are the best is mean you are the no. 1.16:The world <Happy> is the same with "glad".18:google is the best tools for search keyword.

例二:用中括号[]来查找
如果想查找test或taste这两个单词,发现它们的共同点是’t?st’。可以这样查找:

1234

grep -n 't[ae]st' r.txt8:I can't finish the test.9:Oh! The soup taste good.

[]不论有几个字符,它都只代表某“一个”字符。如果想查找有oo的字符:

12345678

grep -n 'oo' r.txt1:"Open Source" is a good mechanism to develop programs.2:apple is my favorite food.3:Football game is not use feet only.9:Oh! The soup taste good.18:google is the best tools for search keyword.19:goooooogle yes!

如果不想要oo前面有g的话:

12345

grep -n '[^g]oo' r.txt2:apple is my favorite food.3:Football game is not use feet only.18:google is the best tools for search keyword.19:goooooogle yes!

如果想要oo前面有小写字母:

12

grep -n '[^a-z]oo' r.txt3:Football game is not use feet only.

类似想法还有:[a-z]、[A-Z]、[0-9]、[a-zA-Z0-9]等,例如:

123

grep -n '[0-9]' r.txt5:However, this dress is about $ 3183 dollars.15:You are the best is mean you are the no. 1.

例三:行首与行尾字符^$
只列出行首有the的行:

12

grep -n '^the' r.txt12:the symbol '*' is represented as start.

列出行首是小写字母的行:

12345678

grep -n '^[a-z]' r.txt2:apple is my favorite food.4:this dress doesn't fit me.10:motorcycle is cheap than car.12:the symbol '*' is represented as start.18:google is the best tools for search keyword.19:goooooogle yes!20:go! go! Let's go.

如果要列出行首不是英文字母的行:

123

grep -n '[a-zA-Z]' r.txt1:"Open Source" is a good mechanism to develop programs.21:# I am VBird

注意:^符号用在方括号[]里外是不同的。在[]内表示“反向选择”,在[]外则表示定位在行首。要找出结尾是小数点(.)的行:

12345678910111213

grep -n '.$' r.txt1:"Open Source" is a good mechanism to develop programs.2:apple is my favorite food.3:Football game is not use feet only.4:this dress doesn't fit me.10:motorcycle is cheap than car.11:This window is clear.12:the symbol '*' is represented as start.15:You are the best is mean you are the no. 1.16:The world <Happy> is the same with "glad".17:I like dog.18:google is the best tools for search keyword.20:go! go! Let's go.

小数点在正则表达式中有特殊含义(下面讲),需要用反斜线()转义。第5到9行的结尾也是小数点,怎么没有打印出来?用cat -A将5到9行打印出来:

1234567

cat -An r.txt | head -n 10 | tail -n 6 5 However, this dress is about $ 3183 dollars.^M$ 6 GNU is free air not free beer.^M$ 7 Her hair is very beauty.^M$ 8 I can't finish the test.^M$ 9 Oh! The soup taste good.^M$ 10 motorcycle is cheap than car.$

5~9行是windows(DOS)格式的断行字符(^M$),而第10行是linux格式断行字符。通过这个也就理解了为啥用$符号表示行尾。如果想找出空白行:

12

grep -n '^$' r.txt22:

linux的配置文件中有大量以#开始的注释,如果想不显示空行和注释:

1234567

grep -v '^$' /etc/deluser.conf | grep -v '^#'REMOVE_HOME = 0REMOVE_ALL_FILES = 0BACKUP = 0BACKUP_TO = "."ONLY_IF_EMPTY = 0EXCLUDE_FSTYPES = "(proc|sysfs|usbfs|devpts|tmpfs|afs)"

例四:任意一个字符.与重复字符*
.(小数点):表示一定有一个任意字符;*(星号):表示重复前一个字符0到无穷次;假设要找出g??d的字符串:

1234

grep -n 'g..d' r.txt1:"Open Source" is a good mechanism to develop programs.9:Oh! The soup taste good.16:The world <Happy> is the same with "glad".

假如要列出oo,ooo,oooo等数据,需要用到星号。需要注意的是’o‘表示’’,’o’,’oo’,’ooo’等,即空字符也用’o‘表示。而’oo‘,表示’o’,’oo’,’ooo’等,即至少有一个o。同理,想表示至少两个o用’ooo*’:

1234567

grep -n 'ooo*' r.txt1:"Open Source" is a good mechanism to develop programs.2:apple is my favorite food.3:Football game is not use feet only.9:Oh! The soup taste good.18:google is the best tools for search keyword.19:goooooogle yes!

如何查找两个g之间至少一个o,即gog,goog,gooog等:

123

grep -n 'goo*g' r.txt18:google is the best tools for search keyword.19:goooooogle yes!

如果要查找以g开头以g结尾的字符串,是’gg’吗?正确的应是’g.g’:

123456

grep -n 'g.*g' r.txt1:"Open Source" is a good mechanism to develop programs.14:The gd software is a library for drafting programs.18:google is the best tools for search keyword.19:goooooogle yes!20:go! go! Let's go.

如果只留下英文单词,则:

123

grep -n 'g[a-zA-Z]*g' r.txt18:google is the best tools for search keyword.19:goooooogle yes!

如果查找任意数字:

123

grep -n '[0-9][0-9]*' r.txt5:However, this dress is about $ 3183 dollars.15:You are the best is mean you are the no. 1.

例五:限定连续RE字符范围{}
之前,用.和*来设置0个到无限个重复字符,如果需要限定重复次数呢?这需要用到限定范围的字符{}了。由于在shell中{}有特殊含义,需要用反斜线\进行转义。假如要找到两个o的字符串:

1234567

grep -n 'o{2}' r.txt1:"Open Source" is a good mechanism to develop programs.2:apple is my favorite food.3:Football game is not use feet only.9:Oh! The soup taste good.18:google is the best tools for search keyword.19:goooooogle yes!

假设要要查找g后面2到5个o,然后再接一个g的字符串,则:

12

grep -n 'go{2,5}g' r.txt18:google is the best tools for search keyword.

第19行由于有6个o,导致没有被选择上。
基础正则表达式的总结
RE字符
含义

^word
带查找的字符串(word)在行首

word$
带查找的字符串(word)在行尾

.
代表一定有一个任意字符


转义字符

重复0次到无穷次的前一个字符

[list]
列举出想要选取的字符,如’a[al]y’表示可以查找aay,aly。

[n1-n2]
列举出想要选取的字符范围,如’[0-9]’表示十进制数字字符

[^list]
定义不要的字符或范围,如’[^A-Z]’表示不要大写字符

{n,m}
连续n到m个前一个RE字符

扩展正则表达式
grep使用扩展正则表达式要加-E参数或直接使用egrep别名命令。
RE字符
含义

重复1次到无穷次的前一个字符

?
代表0个或1个任意字符

|
用或(or)的方式找出数个字符串.例如,egrep -n ‘gd| good’ r.txt

()
找出”组”字符串。如查找glad或good, egrep -n ‘g(la| oo)d’ r.txt

()+
重复1次到无穷次前面的组。如查找”AxyzxyzxyzxyzC”,echo ‘AxyzxyzxyzxyzC’ | egrep ‘A(xyz)+C’

需要强调的是感叹号!在正则表达式中并不是特殊字符。
以上,希望有帮助

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容

  • 简介/声明 为什么要写此文呢?稍微有点Web基础的同学应该都知道网页的表单大多都要做表单验证。而正则表达式正好可以...
    Airmole阅读 1,581评论 4 21
  • 正则表达式只是一种表示法,只要工具支持这种表示法, 那么该工具就可以处理正则表达式的字符串。vim、grep、aw...
    水平阅读 1,249评论 0 0
  • 这是个命题作文,“你想成为跨界人才吗?” 跨界说的通俗易懂点儿,就是搞副业!这和星姐:“下班后”这个品牌创立的初衷...
    无尾熊自成长阅读 517评论 0 1
  • 01 清明过完,学生们也陆陆续续的返校了。同寝室的小Y同学却一脸不高兴的样子,叹了口气就开始和我们抱怨。她说,她的...
    汪星人的牛奶盒阅读 393评论 0 0
  • 刚才看了一个综艺节目,想必大家都知道,叫“大学生来了”,看到这期是谈“工具人”的。 什么是工具人,大概就是做着备...
    就要暖暖的阅读 331评论 0 0