正则表达式练习

练习文本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
"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

1.匹配’the’.不论大小写

1
2
3
[root@localhost ~]$grep -i "the" zz.txt  
I can't finish the test.
Oh! The soup taste good.

2.匹配tast和test.

1
2
3
[root@localhost ~]$grep 't[ae]st' zz.txt  
I can't finish the test.
Oh! The soup taste good.

3.匹配不包含g的”oo”字符串

1
2
3
4
5
[root@localhost ~]$grep "[^g]oo" zz.txt
apple is my favorite food.
Football game is not use feet only.
google is the best tools for search keyword.
goooooogle yes!

这里倒数第二行实际是匹配到tools,而最后一行匹配的不是goo这个g开头的字符串,而是ooo.

4.匹配有数字的行

1
2
3
[root@localhost ~]$grep "[0-9]" zz.txt
However, this dress is about $ 3183 dollars.
You are the best is mean you are the no. 1.

还可以用perl正则来实现:

1
2
3
[root@localhost ~]$grep -P "\d"  zz.txt
However, this dress is about $ 3183 dollars.
You are the best is mean you are the no. 1.

grep如果要使用perl正则 需要加上-P参数

5.匹配开头是the单词的一行

1
2
[root@localhost ~]$grep  "^the"  zz.txt
the symbol '*' is represented as start.

6.查找不以小写字母开头的行

1
2
3
4
5
6
7
[root@localhost ~]$grep  "^[^a-z]"  zz.txt
"Open Source" is a good mechanism to develop programs.
Football game is not use feet only.
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.

当\^在前面,表示匹配开头,当\^在[]中括号里面,表示取反

7.匹配小数点结尾的行.

1
2
3
[root@localhost ~]$grep "\.$" zz.txt
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.

或者

1
2
3
[root@localhost ~]$grep  '\.\B' zz.txt
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
  1. 匹配g开头,结尾是d,的四个字符.也就是g..d
1
2
3
4
[root@localhost ~]$grep  "g..d" zz.txt
"Open Source" is a good mechanism to develop programs.
Oh! The soup taste good.
The world <Happy> is the same with "glad".

9.找出2个0以上的字符串

1
2
3
4
5
6
7
[root@localhost ~]$grep  "ooo*" zz.txt
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
Oh! The soup taste good.
google is the best tools for search keyword.
goooooogle yes!

或者:

1
2
3
4
5
6
7
[root@localhost ~]$grep  "o\{2,\}" zz.txt
"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
Oh! The soup taste good.
google is the best tools for search keyword.
goooooogle yes!

因为{}括号有特殊的意义,所以需要用转义字符\来让他失去特殊意义

10.查找g开头,g结尾,中间2-5个o的字符串….

1
2
[root@localhost ~]$grep  "^go\{2,5\}g" zz.txt
google is the best tools for search keyword.

本文标题:正则表达式练习

文章作者:Jesse

发布时间:2018年06月12日 - 22:06

最后更新:2018年12月01日 - 22:12

原始链接:https://jesse.top/2018/06/12/Linux-Basic/正则表达式/正则表达式练习/

许可协议: 禁止商业用途 转载请保留原文链接及作者。

坚持原创技术分享,您的支持将鼓励我继续创作!