首页
登录 | 注册

Linux学习基础之Shell编程——正则表达式

1、正则表达式与通配符

》正则表达式用来在文件中匹配符合条件的字符串,正则是包含匹配。grep、awk、sed等命令可以支持正则表达式。

》通配符用来匹配符合条件的文件名,通配符是完全匹配。ls、find、cp等这些命令不支持正则表达式,所以只能使用shell自己的通配符来进行匹配了。

注:Linux中正则和通配符的区别比较大,其他编程语言中,通配符属于正则中的一部分。

2、基础正则表达式:

元字符 作用
* 前一个字符匹配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位的小写字母

注意:

1)通配符中的* 号和正则中的* 号有区别;

2)正则中的. 相当于通配符中的?

如下测试文档内容:

[root@localhost sh]# vim zhengzetest

Mr. XiaoxiaoZhou said:

he was the honest man in his School.

123 despise him.

But since Mr. Li lei came,
he never saaaid those words.
5555nice!

because,actunaaaally,
Mr. Li lei is teh most honest man!

Later,Mr. XiaoxiaoZhou sold his hot body.


~                                          

示例1、

“ * ”   前一个字符匹配0次,或任意多次

》grep "a*" test_rules.txt

#匹配所有内容,包括空白行

》grep "aa*" test_rules.txt

#匹配至少包含有一个a的行

》grep "aaa*" test_rules.txt

#匹配最少包含两个连续a的字符

》grep "aaaaa*" test_rules.txt

#则会匹配最少包含四个连续a的字符串

[root@localhost sh]# grep "a*" zhengzetest 
Mr. XiaoxiaoZhou said:

he was the honest man in his School.

123 despise him.

But since Mr. Li lei came,
he never saaaid those words.
5555nice!

because,actunaaaally,
Mr. Li lei is teh most honest man!

Later,Mr. XiaoxiaoZhou sold his hot body.


[root@localhost sh]# grep "aa*" zhengzetest 
Mr. XiaoxiaoZhou said:
he was the honest man in his School.
But since Mr. Li lei came,
he never saaaid those words.
because,actunaaaally,
Mr. Li lei is teh most honest man!
Later,Mr. XiaoxiaoZhou sold his hot body.
[root@localhost sh]# grep "aaa*" zhengzetest 
he never saaaid those words.
because,actunaaaally,
[root@localhost sh]# grep "aaaaa*" zhengzetest 
because,actunaaaally,
[root@localhost sh]# 

示例2、“ . ” 匹配除了换行符外任意一个字符

》grep "s..d" test_rules.txt

#"s..d" 会匹配在s和d这两个字母之间一定有两个字符的单词

》grep "s.*d" test_rules.txt

#匹配在s和d字母之间有任意字符

》grep ".*" test_rules.txt

#匹配所有内容

[root@localhost sh]# grep "s..d" zhengzetest 
Mr. XiaoxiaoZhou said:
Later,Mr. XiaoxiaoZhou sold his hot body.
[root@localhost sh]# grep "s.*d" zhengzetest 
Mr. XiaoxiaoZhou said:
he never saaaid those words.
Later,Mr. XiaoxiaoZhou sold his hot body.
[root@localhost sh]# grep ".*" zhengzetest 
Mr. XiaoxiaoZhou said:

he was the honest man in his School.

123 despise him.

But since Mr. Li lei came,
he never saaaid those words.
5555nice!

because,actunaaaally,
Mr. Li lei is teh most honest man!

Later,Mr. XiaoxiaoZhou sold his hot body.


[root@localhost sh]# 

示例3、“^”匹配行首,“$”匹配行尾

》grep "^M" zhengzetest

#匹配以大写“M” 开头的行

》grep “n$”  zhengzetest

#匹配以小写“n”结尾的行

》grep -n "^$" zhengzetest

#会匹配空白行

[root@localhost sh]# grep "^M" zhengzetest 
Mr. XiaoxiaoZhou said:
Mr. Li lei is teh most honest man!
[root@localhost sh]# grep "n$" zhengzetest 
[root@localhost sh]# grep -n "^$" zhengzetest 
2:
4:
6:
10:
13:
15:
16:
[root@localhost sh]# vim zhengzetest
[root@localhost sh]# grep "e$" zhengzetest 
[root@localhost sh]# vim zhengzetest
[root@localhost sh]# grep ",$" zhengzetest 
But since Mr. Li lei came,
because,actunaaaally,
[root@localhost sh]# 

示例4、“[  ]”匹配中括号中指定的任意一个字符,只匹配一个字符

》grep "s[ao]id" zhengzetest

#匹配s和i字母中,要不是a,要不是o

》grep“[0-9]” zhengzetest

#匹配任意一个数字

》grep "^[a-z]" zhengzetest

#匹配用小写字母开头的行

[root@localhost sh]# 
[root@localhost sh]# grep "s[ao]id" zhengzetest 
Mr. XiaoxiaoZhou said:
[root@localhost sh]# grep "[0-9]" zhengzetest 
123 despise him.
5555nice!
[root@localhost sh]# grep "^[a-z]" zhengzetest 
he was the honest man in his School.
he never saaaid those words.
because,actunaaaally,
[root@localhost sh]# 

示例5、“[^ ]”匹配除中括号内的字符以外的任意一个字符

》grep "^[^a-z]" zhengzetest

#匹配不用小写字母开头的行

》grep "^[^a-zA-Z]" zhengzetest

#匹配不用字母开头的行

[root@localhost sh]# grep "^[^a-z]" zhengzetest 
Mr. XiaoxiaoZhou said:
123 despise him.
But since Mr. Li lei came,
5555nice!
Mr. Li lei is teh most honest man!
Later,Mr. XiaoxiaoZhou sold his hot body.
[root@localhost sh]# grep "^[^a-zA-Z" zhengzetest 
grep:  不匹配的 [ 或 [^
[root@localhost sh]# grep "^[^a-zA-Z]" zhengzetest 
123 despise him.
5555nice!
[root@localhost sh]# 

示例6、“ \”转义符

》grep "\.$" zhengzetest     

#匹配使用“ . ” 结尾的行   因为. 在正则表达式或者说shell中有特殊含义,想要使用

其最普通的英文句号的功能,只能用转义符将其在shell中的特殊含义去掉,再用

[root@localhost sh]# grep "\.$" zhengzetest 
he was the honest man in his School.
123 despise him.
he never saaaid those words.
Later,Mr. XiaoxiaoZhou sold his hot body.
[root@localhost sh]# 

示例7、“\{n\}” 表示其前面的字符恰好出现n次

》grep "a\{3\}" zhengzetest

#匹配a字母连续出现三次的字符串

》grep "[0-9]\{3}" zhengzetest

#匹配包含连续的三个数字的字符串

[root@localhost sh]# grep "a\{3\}" zhengzetest 
he never saaaid those words.
because,actunaaaally,
[root@localhost sh]# grep "[0-9]\{3\}" zhengzetest 
123 despise him.
5555nice!
[root@localhost sh]# 

示例8、“\{n,m\}” 匹配其前面的字符至少出现n次最多出现m次

》grep "sa\{1,3\}i" zhengzetest

#匹配在字母s和字母i之间有最少一个a,最多三个a

[root@localhost sh]# grep "sa\{1,3\}" zhengzetest 
Mr. XiaoxiaoZhou said:
he never saaaid those words.

 



2020 jeepxie.net webmaster#jeepxie.net
10 q. 0.008 s.
京ICP备10005923号