正则表达式--grep

练习例子

"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)的方式找出数个字符串
()找出 "组" 字符串
()+多个重复组的判别

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值