练习例子
"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. 查找简单字符串
grep -n "the" regular_express.txt
2. 反向查找(没有 "the" 的行)
二、利用中括号 [] 来查找集合字符串
1. 查找 test 或 taste
grep -n "t[ae]st" regular_express.txt
2. 查找 "oo"
(1)所有 "oo"
grep -n "oo" regular_express.txt
(2)前面没有 "g"4
grep -n "[^g]oo" regular_express.txt
(3)前面没有小写字母
grep -n "[^a-z]oo" regular_express.txt
grep -n "[^[:lower:]]oo" regular_express.txt
(4)有数字
grep -n "[0-9]" regular_express.txt
三、行首与行尾字符^$
注意:"^" 在 "[]" 中是反向的意思,之外是行首
1. 只在行首的 "the"
grep -n "^the" regular_express.txt
2. 开头是小写字符
grep -n "^[a-z]" regular_express.txt
3. 开头不是英文字母
grep -n "^[a-z]" regular_express.txt
4. 行尾结束是小数点(\ 为转义字符)
grep -n "\.$" regular_express.txt
5. 空白行
grep -n "^$" regular_express.txt
四、任意字符. 重复字符*
. :一定有一个任意字符
* :重复前一个0到无穷多次
1. 找出 "g??d"
grep -n "g..d" regular_express.txt
2. 至少有两个 "o"
grep -n "ooo*" regular_express.txt
3. 查找 "g ... g"
grep -n "g.*g" regular_express.txt
4. 查找任意数字
grep -n "[0-9]*[0-9]" regular_express.txt
五、限定任意连续 RE 字符串{}
1. 找到两个 "o" 的字符串
grep -n "o\{2\}" regular_express.txt
2. 查找 'g' 后面有两个到五个 "o" 的字符串
grep -n "go\{2,5\}g" regular_express.txt
2. 查找 'g' 后面有两个以上 "o" 的字符串
grep -n "go\{2,\}g" regular_express.txt
六、总结
字符 | 意义 |
^word | 查找行首 |
word$ | 查找行末 |
. | 一定有一个任意字符 |
\ | 转义字符 |
* | 重复0个到无穷个的前一个字符 |
[list] | 找出想要选取的字符 |
[n1-n2] | 找出想要选取的字符范围 |
[^list] | 找出不想要的字符串或范围 |
\{n,m\} | n到m个前一个字符 |
扩展正则表达式
+ | 重复一个或一个以上的 RE 字符 |
? | 零个或一个的前一个 RE 字符 |
| | 用或(or)的方式找出数个字符串 |
() | 找出 "组" 字符串 |
()+ | 多个重复组的判别 |