CC00001.LinuxShell——|Linux&Shell&正则表达式.V01|

一、正则表达式
### --- 正则表达式概述

~~~     还记得我们在上一章说过正则表达式和通配符的区别
~~~     #(正则表达式用来在文件中匹配符合条件的字符串,通配符用来匹配符合条件的文件名)吗?
~~~     其实这种区别只在 Shell 当中适用,因为用来在文件当中搜索字符串的命令,
~~~     如 grep、awk、sed 等命令可以支持正则表达式,而在系统当中搜索文件的命令,
~~~     如 ls、find、cp 这些命令不支持正则表达式,所以只能使用 shell 自己的通配符来进行匹配了。

二、基础正则表达式

元字符作用
*前一个字符匹配 0 次或任意多次。
.匹配除了换行符外任意一个字符。
^匹配行首。例如:^hello 会匹配以 hello 开头的行。
$匹配行尾。例如:hello&会匹配以 hello 结尾的行。
[]

匹配中括号中指定的任意一个字符,只匹配一个字符。

例如:[aoeiu] 匹配任意一个元音字母,

[0-9] 匹配任意一位数字,[a-z][0-9]匹配小写字和一位数字构成的两位字符。

[^]

匹配除中括号的字符以外的任意一个字符。

例如:[^0-9] 匹配任意一位非数字字符,[^a-z] 表示任意一位非小写字母。

\转义符。用于取消讲特殊符号的含义取消。
\{n\}

表示其前面的字符恰好出现 n 次。

例如:[0-9]\{4\} 匹配 4 位数字,[1][3-8][0-9]\{9\} 匹配手机号码。

\{n,\}

表示其前面的字符出现不小于 n 次。例如: [0-9]\{2,\} 表示两位及以上的数字。

\{n,m\}

表示其前面的字符至少出现 n 次,最多出现 m 次。

例如:[a-z]\{6,8\}匹配 6 到 8 位的小写字母。

三、正则表达式建立文件

### --- 建立别名
~~~     在~/.bashrc 文件中建立这个别名:

[root@localhost ~]# vi /root/.bashrc
alias grep='grep --color=auto'
### --- 练习文件建立
~~~     练习文件建立

[root@localhost ~]# vi test_rule.txt
Mr. Li Ming said:
he was the most honest man.
123despise him.
But since Mr. shen Chao came,
he never saaaid those words.
5555nice!
because,actuaaaally,
Mr. Shen Chao is the most honest man
Later,Mr. Li ming soid his hot body.
四、前一个字符匹配0次或者任意多次
### --- “*”前一个字符匹配 0 次,或任意多次“*”前一个字符匹配 0 次,或任意多次
~~~     “*”前一个字符匹配 0 次,或任意多次
~~~     注:如果这样写正则表达式“aa*”代表这行字符串一定要有一个 a,
~~~     但是后面有没有 a 都可以。也就是说

[root@localhost ~]# grep "a*" test_rule.txt
[root@localhost ~]# grep "a*" test_rule.txt
Mr. Li Ming said:
he was the most honest man.
123despise him.
But since Mr. shen Chao came,
he never saaaid those words.
5555nice!
because,actuaaaally,
Mr. Shen Chao is the most honest man
Later,Mr. Li ming soid his hot body.
### --- 会匹配至少包含有一个 a 的行:会匹配至少包含有一个 a 的行:
### --- 会匹配至少包含有一个 a 的行:

[root@localhost ~]# grep "aa*" test_rule.txt
Mr. Li Ming said:
he was the most honest man.
But since Mr. shen Chao came,
he never saaaid those words.
because,actuaaaally,
Mr. Shen Chao is the most honest man
Later,Mr. Li ming soid his hot body.
### --- 如果正则表达式是“aaa*”,则会匹配最少包含两个连续 a 的字符串
### --- 如果正则表达式是“aaa*”,则会匹配最少包含两个连续 a 的字符串,如:

[root@localhost ~]# grep "aaa*" test_rule.txt
he never saaaid those words.
because,actuaaaally,
### --- 如果正则表达式是“aaaaa*”,则会匹配最少包含四个个连续 a 的字符串
### --- 如果正则表达式是“aaaaa*”,则会匹配最少包含四个个连续 a 的字符串,如:
~~~     注:当然如果再多写一个 a,
~~~     如“aaaaaa*”就不能从这篇文档中匹配任何内容了,因为我们这篇文档
~~~     注:中 a 最多的单词“actuaaaally”只有四个个连续的 a,
~~~     而“aaaaaa*”会匹配最少五个连续的 a。

[root@localhost ~]# grep "aaaaa*" test_rule.txt
because,actuaaaally,
五、匹配除了换行符外任意一个字符
### --- “.” 匹配除了换行符外任意一个字符
~~~     正则表达式“.”只能匹配一个字符,这个字符可以是任意字符,举个例子:

[root@localhost ~]# grep "s..d" test_rule.txt
Mr. Li Ming said:
Later,Mr. Li ming soid his hot body.
### --- “s..d”会匹配在 s 和 d 这两个字母之间一定有两个字符的单词

[root@localhost ~]# grep "s.*d" test_rule.txt
Mr. Li Ming said:
he never saaaid those words.
Later,Mr. Li ming soid his hot body.
### --- 最后一句话比较有意思,匹配的是“soid his hot bod”

[root@localhost ~]# grep ".*" test_rule.txt
[root@localhost ~]# grep ".*" test_rule.txt
Mr. Li Ming said:
he was the most honest man.
123despise him.
But since Mr. shen Chao came,
he never saaaid those words.
5555nice!
because,actuaaaally,
Mr. Shen Chao is the most honest man
Later,Mr. Li ming soid his hot body.
六、匹配行首,匹配行尾
### --- “^”代表匹配行首,比如“^M”会匹配以大写“M”开头的行:

[root@localhost ~]# grep "^M" test_rule.txt
Mr. Li Ming said:
Mr. Shen Chao is the most honest man
### --- “$”代表匹配行尾,如果“n$”会匹配以小写“n”结尾的行:

[root@localhost ~]# grep "n$" test_rule.txt
Mr. Shen Chao is the most honest man
### --- 而“^$”则会匹配空白行:

[root@localhost ~]# grep -n "^$" test_rule.txt
### --- “[]” 匹配中括号中指定的任意一个字符,只匹配一个字符

~~~     “[]”会匹配中括号中指定任意一个字符,注意只能匹配一个字符。
~~~     比如[ao]要不会匹配一个 a
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yanqi_vip

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值