正则表达式基本用法(持续更新中)

简介

表达式是处理字符串的方法,它以行(háng)为单位来进行字符串的处理操作。正则表达式通过一些符号的辅助,可以轻松完成“查”、“删”、“换”的功能。

严谨地说,正则表达式是一种表示法,只要程序(比如vi、grep、awk、sed等常用命令工具,以及python的re模块)支持正则表达式,就可以使用这种方法处理字符串。

注意:正则表达式与通配符完全不同。通配符是bash操作的接口,而正则表达式是一种字符串的处理方式。

实验环境:操作系统Ubuntu
18.04.3 LTS (GNU/Linux 5.0.0-36-generic x86_64)
在VMWare WorkStation 15 Pro下运行

使用 Linux shell的grep命令练习基本的正则表达式

使用了《鸟叔的Linux私房菜》中的例子文本
使用以下命令行操作下载例子文本

 wget http://linux.vbird.org/linux_basic/0330regularex/regular_express.txt  

1、查找特定字符串

例子如下:用显示行号的方式查找regular_express.txt中含有’the’的字符串。

os@ubuntu:~$ grep -n 'the' regular_express.txt 
8:I can not 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.

反向选择。例子如下:(查找regular_express.txt中不含有’the’的字符串)显然,是除了8,12,15,16,18以外的 所有行。
(例子如下)

os@ubuntu:~$ grep -vn 'the' regular_express.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 does not fit me.
5:However, this dress is about $ 3183 dollars.
6:GNU is free air not free beer.
7:Her hair is very beauty.
9:Oh! The soup taste good.
10:motorcycle is cheap than car.
11:This window is clear.
13:Oh!  My god!
14:The gd software is a library for drafting programs.
17:I like dog.
19:goooooogle yes!
20:go! go! Let's go.
21:# I am VBird
22:

2、用[ ]查找有共同点的字符

(1)查找有共同点的字符串

例子如下,查找regular_express.txt中含有’tast’或者’test’的字符串。

os@ubuntu:~$ grep -n 't[ae]st' regular_express.txt 
8:I can't finish the test.
9:Oh! The soup taste good.

(注意:[ ]中无论含有多少字符一次智能代表一个字符)

(2)实现反向选择

用显示行号的方式查找regular_express.txt中含有’[ ]he’、但’he’前面不是t的字符串。

os@ubuntu:~$ grep -n '[^t]he' regular_express.txt 
9:Oh! The soup taste good.
10:motorcycle is cheap than car.
14:The gd software is a library for drafting programs.
16:The world <Happy> is the same with "glad".

查找开头为数字的字符串:

os@ubuntu:~$ grep -n '[0-9]' regular_express.txt 
5:However, this dress is about $ 3183 dollars.
15:You are the best is mean you are the no. 1.

(第一个例子)查找字符串’[ _ ] oo’(oo前有一个字符),但是开头不为小写字母;

os@ubuntu:~$ grep -n '[^[:lower:]]oo' regular_express.txt 
3:Football game is not use feet only.

(第二个例子)查找开头为大写字母的字符串:

os@ubuntu:~$ grep -n '^[[:upper:]]' regular_express.txt  
3:Football game is not use feet only.
5:However, this dress is about $ 3183 dollars.
6:GNU is free air not free beer.
7:Her hair is very beauty.
8:I can't finish the test.
9:Oh! The soup taste good.
11:This window is clear.
13:Oh!  My god!
14:The gd software is a library for drafting programs.
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.

查找开头既不是大写字母也不是小写字母的字符串:

os@ubuntu:~$ grep -n '^[^a-zA-Z]' regular_express.txt 
1:"Open Source" is a good mechanism to develop programs.
21:# I am VBird

这种做法同样可以表示为:

os@ubuntu:~$ grep -n '^[^[:alpha:]]' regular_express.txt         
1:"Open Source" is a good mechanism to develop programs.
21:# I am VBird

注意:
1、上述例子中的 alpha(所有大小写字母)、lower(所有小写字母)表示迭代。
2、’^’在[ ]内表示反向选择,在[ ]外表示定位于行首(而定位于行尾定义为$)

上面的例子表示查找以’ . ‘结尾的行,并且显示行号

os@ubuntu:~$ grep -n '\.$' regular_express.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 does not 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.

(因为‘ . ’比较特殊,需要使用转义符表示,同样需要使用转义符使其失去特殊意义的还有 花括号{ } 、单引号 ’ 等)

3、正则表达式中的特殊字符

’ . ‘小数点表示“一个任意字符”

‘ * ’星号表示“重复前一个字符,零次到无穷多次”

例子如下:

查找含四个字符的字符串,开头为g,结尾为d

os@ubuntu:~$ grep -n 'g..d' regular_express.txt 
1:"Open Source" is a good mechanism to develop programs.
9:Oh! The soup taste good.
16:The world <Happy> is the same with "glad".

找开头为g,结尾为g,但是中间至少存在一个o的字符串

os@ubuntu:~$ grep -n 'goo*g' regular_express.txt    
18:google is the best tools for search keyword.
19:goooooogle yes!

查找开头为g,结尾为g,但是中间的字符可有可无的字符串

os@ubuntu:~$ grep -n 'g.*g' regular_express.txt      
1:"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.

查找含数字的所有行

os@ubuntu:~$ grep -n '[[:digit:]][[:digit:]]*' regular_express.txt       
5:However, this dress is about $ 3183 dollars.
15:You are the best is mean you are the no. 1.

4、限定字符的范围(使用 { 和 } )

查找开头为g,结尾为g,中间有2-5个o的字符串

os@ubuntu:~$ grep -n 'go\{2,5\}g' regular_express.txt 
18:google is the best tools for search keyword.

查找开头为g,结尾为g,中间有2个以上o的字符串

os@ubuntu:~$ grep -n 'go\{2,\}g' regular_express.txt  
18:google is the best tools for search keyword.
19:goooooogle yes!


如有错误,希望各位技术大神不吝赐教

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值