|NO.Z.00001|——————————|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

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

posted on   yanqi_vip  阅读(25)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示