正则表达式详解
正则表达式主要用来处理字串,对特定字串进行『搜寻/删除/取代』操作!
正则表达式和通配符
正则表达式
- 正则表达式用来在文件中匹配符合条件的字符串。
- 正则是包含匹配
- grep、 awk、 sed、 等命令支持正则表达式
通配符
相关linux通配符详细内容参考:https://blog.csdn.net/lingyiwin/article/details/121757754
- 通配符用来匹配符合条件的文件名。
- 通配符是完全匹配
- ls、 find、 cp 这些命令不支持正则表达式,但支持通配符。
基础正则表示法
元字符 | 作用 |
---|---|
* | 前一个字符匹配0次或者任意次 |
. | 匹配除了换行符外任意一个字符 |
^ | 匹配行首 |
$ | 匹配行尾 |
[] | 匹配中括号中任意一个字符,只匹配一个字符 |
[n1-n2] | 字节集合的 RE 字符,里面列出想要撷取的字节范围!例如所有大写字节则为 [A-Z] : grep ‘[A-Z]’ file |
[^] | 匹配除中括号中的字符以外的任意一个字符 |
\ | 转义符;取消特殊符号的含义 |
\{n\} | 前面字符恰好出现n次;[0-9]\{4\}:匹配4位数字;[1][3-8][0-9]\{9\} :匹配手机号 |
\{n,\} | 前面字符恰好出现不小于n次 |
\{n,m\} | 前面字符恰好出现不小于n次,且不大于m次(<=)。 |
下面通过示例实操一下。
regular.txt 内容来自鸟哥linux私房菜,再此也推荐大家去阅读一下这本书。
[userwin@MiWiFi-R3L-srv rule]$ vim regular.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.^M
GNU is free air not free beer.^M
Her hair is very beauty.^M
I can't finish the test.^M
Oh! The soup taste good.^M
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.^M
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
gogle
gooogle
goooogle
* 前一个字符匹配0次或者任意次
goo*g : 前面的字符o代表元字符,它的含义是至少包含一个o
gooog : *前面的字符o代表元字符,它的含义是至少包含2个o
# 匹配包含 gog 的行; -n打印行号
[userwin@MiWiFi-R3L-srv rule]$ grep -n 'gog' regular.txt
22:gogle
# 匹配包含 goog 的行
[userwin@MiWiFi-R3L-srv rule]$ grep -n 'goog' regular.txt
18:google is the best tools for search keyword.
# 匹配包含 go{o* 中间至少包含一个o}g 的行
[userwin@MiWiFi-R3L-srv rule]$ grep -n 'goo*g' regular.txt
18:google is the best tools for search keyword.
19:goooooogle yes!
22:gogle
23:gooogle
24:goooogle
# 匹配包含 goo{o* 中间至少包含2个o}g 的行
[userwin@MiWiFi-R3L-srv rule]$ grep -n 'gooo*g' regular.txt
18:google is the best tools for search keyword.
19:goooooogle yes!
23:gooogle
24:goooogle
# 匹配包含 gooo{o* 中间至少包含3个o}g 的行
[userwin@MiWiFi-R3L-srv rule]$ grep -n 'goooo*g' regular.txt
19:goooooogle yes!
23:gooogle
24:goooogle
# 匹配包含 goooo{o* 中间至少包含4个o}g 的行
[userwin@MiWiFi-R3L-srv rule]$ grep -n 'gooooo*g' regular.txt
19:goooooogle yes!
24:goooogle
# 匹配包含 gooooo{o* 中间至少包含5个o}g 的行
[userwin@MiWiFi-R3L-srv rule]$ grep -n 'goooooo*g' regular.txt
19:goooooogle yes!
[userwin@MiWiFi-R3L-srv rule]$
. 匹配除了换行符外任意一个字符
# 任意一个字符
[userwin@MiWiFi-R3L-srv rule]$ grep -n 'g.g' regular.txt
22:gogle
# 任意2个字符
[userwin@MiWiFi-R3L-srv rule]$ grep -n 'g..g' regular.txt
18:google is the best tools for search keyword.
# 任意3个字符
[userwin@MiWiFi-R3L-srv rule]$ grep -n 'g...g' regular.txt
20:go! go! Let's go.
23:gooogle
# 任意4个字符
[userwin@MiWiFi-R3L-srv rule]$ grep -n 'g....g' regular.txt
14:The gd software is a library for drafting programs.^M
24:goooogle
[userwin@MiWiFi-R3L-srv rule]$
^ 匹配行首
[userwin@MiWiFi-R3L-srv rule]$ grep -n '^the' regular.txt
12:the symbol '*' is represented as start.
[userwin@MiWiFi-R3L-srv rule]$ grep -n '^The' regular.txt
14:The gd software is a library for drafting programs.^M
16:The world <Happy> is the same with "glad".
# 匹配开头为a-z 小写字符的行
[userwin@MiWiFi-R3L-srv rule]$ grep -n '^[a-z]' regular.txt
2: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.
22:gogle
23:gooogle
24:goooogle
# 匹配开头为a-z 小写字符的行
[userwin@MiWiFi-R3L-srv rule]$ grep -n '^[[:lower:]]' regular.txt
2: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.
22:gogle
23:gooogle
24:goooogle
# 匹配开头为大写字符的行
[userwin@MiWiFi-R3L-srv rule]$ grep -n '^[[:upper:]]' regular.txt
3:Football game is not use feet only.
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
11:This window is clear.
13:Oh! My god!
14:The gd software is a library for drafting programs.^M
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.
# 匹配开头为英文字符的行
[userwin@MiWiFi-R3L-srv rule]$ grep -n '^[a-zA-Z]' regular.txt
2:apple is my favorite food.
3:Football game is not use feet only.
4:this dress doesn't fit me.
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.
11:This window is clear.
12:the symbol '*' is represented as start.
13:Oh! My god!
14:The gd software is a library for drafting programs.^M
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.
19:goooooogle yes!
20:go! go! Let's go.
22:gogle
23:gooogle
24:goooogle
$ 匹配行尾
# 匹配以.结尾的行 \. 点需要转义符转义
[userwin@MiWiFi-R3L-srv rule]$ grep -n '\.$' regular.txt
1:"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.
# 查看 regular.txt
[userwin@MiWiFi-R3L-srv rule]$ cat -An regular.txt
1 "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.$
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.$
11 This window is clear.$
12 the symbol '*' is represented as start.$
13 Oh! My god!$
14 The gd software is a library for drafting programs.^M$
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.$
19 goooooogle yes!$
20 go! go! Let's go.$
21 # I am VBird$
22 gogle$
23 gooogle$
24 goooogle$
25 $
# 匹配前10行
[userwin@MiWiFi-R3L-srv rule]$ cat -An regular.txt | head -n 10
1 "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.$
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.$
#匹配前10行后再匹配后6行
[userwin@MiWiFi-R3L-srv rule]$ cat -An regular.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.$
[] 匹配中括号中任意一个字符,只匹配一个字符
# 匹配包含tast或test 的行
[userwin@MiWiFi-R3L-srv rule]$ grep -n 't[ae]st' regular.txt
8:I can't finish the test.^M
9:Oh! The soup taste good.^M
# 匹配包含 goog gooo 的行
[userwin@MiWiFi-R3L-srv rule]$ grep -n 'goo[go]' regular.txt
18:google is the best tools for search keyword.
19:goooooogle yes!
23:gooogle
24:goooogle
# 匹配包含goo之后字符不是g或o字符的行
[userwin@MiWiFi-R3L-srv rule]$ grep -n 'goo[^go]' regular.txt
1:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good.^M
[n1-n2] 字节集合的 RE 字符,里面列出想要撷取的字节范围!例如所有大写字节则为 [A-Z] : grep ‘[A-Z]’ file
# 匹配开头为a-z 小写字符的行
[userwin@MiWiFi-R3L-srv rule]$ grep -n '^[a-z]' regular.txt
2: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.
22:gogle
23:gooogle
24:goooogle
[userwin@MiWiFi-R3L-srv rule]$ grep -n '[0-9][0-9]*' regular.txt
5:However, this dress is about $ 3183 dollars.^M
15:You are the best is mean you are the no. 1.
[^] 匹配除中括号中的字符以外的任意一个字符
# 匹配包含goo之后字符不是g或o字符的行
[userwin@MiWiFi-R3L-srv rule]$ grep -n 'goo[^go]' regular.txt
1:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good.^M
{n} 前面字符恰好出现n次
[0-9]\{4\}:匹配4位数字;[1][3-8][0-9]\{9\} :匹配手机号
[userwin@MiWiFi-R3L-srv rule]$ grep -n 'o\{2\}' regular.txt
1:"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.^M
18:google is the best tools for search keyword.
19:goooooogle yes!
23:gooogle
24:goooogle
[userwin@MiWiFi-R3L-srv rule]$ grep -n 'o\{3\}' regular.txt
19:goooooogle yes!
23:gooogle
24:goooogle
[userwin@MiWiFi-R3L-srv rule]$ grep -n 'o\{4\}' regular.txt
19:goooooogle yes!
24:goooogle
[userwin@MiWiFi-R3L-srv rule]$ grep -n 'o\{5\}' regular.txt
19:goooooogle yes!
{n,} 前面字符恰好出现不小于n次
[userwin@MiWiFi-R3L-srv rule]$ grep -n 'go\{2,\}g' regular.txt
18:google is the best tools for search keyword.
19:goooooogle yes!
23:gooogle
24:goooogle
[userwin@MiWiFi-R3L-srv rule]$ grep -n 'go\{5,\}g' regular.txt
19:goooooogle yes!
{n,m} | 前面字符恰好出现不小于n次,且不大于m次(<=)
[userwin@MiWiFi-R3L-srv rule]$ grep -n 'go\{2,5\}g' regular.txt
18:google is the best tools for search keyword.
23:gooogle
24:goooogle
[userwin@MiWiFi-R3L-srv rule]$ grep -n 'go\{1,2\}g' regular.txt
18:google is the best tools for search keyword.
22:gogle
正则表达式特殊符号
特殊符号 | 代表意义 |
---|---|
[:alnum:] | 代表英文大小写字节及数字,亦即 0-9, A-Z, a-z |
[:alpha:] | 代表任何英文大小写字节,亦即 A-Z, a-z |
[:blank:] | 代表空白键与 [Tab] 按键两者 |
[:cntrl:] | 代表键盘上面的控制按键,亦即包括 CR, LF, Tab, Del… 等等 |
[:digit:] | 代表数字而已,亦即 0-9 |
[:graph:] | 除了空白字节 (空白键与 [Tab] 按键) 外的其他所有按键 |
[:lower:] | 代表小写字节,亦即 a-z |
[:print:] | 代表任何可以被列印出来的字节 |
[:punct:] | 代表标点符号 (punctuation symbol),亦即:" ’ ? ! ; : # $… |
[:upper:] | 代表大写字节,亦即 A-Z |
[:space:] | 任何会产生空白的字节,包括空白键, [Tab], CR 等等 |
[:xdigit:] | 代表 16 进位的数字类型,因此包括: 0-9, A-F, a-f 的数字与字节 |
匹配空白行
重点:单独列出来
# 匹配 开头为空,结尾也为空
[userwin@MiWiFi-R3L-srv rule]$ grep -n '^$' regular.txt
25:
推荐阅读《鸟哥linux私房菜》第十二章;收益良多。
书山有路勤为径,学海无涯苦作舟
此文花费四个小时编辑,多多点赞评论,非常感谢。