1. 程式人生 > >Linux學習基礎之Shell程式設計——正則表示式

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)正則中的. 相當於萬用字元中的?

如下測試文件內容:

[[email protected] 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的字串

[[email protected] 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.


[[email protected] 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.
[[email protected] sh]# grep "aaa*" zhengzetest 
he never saaaid those words.
because,actunaaaally,
[[email protected] sh]# grep "aaaaa*" zhengzetest 
because,actunaaaally,
[[email protected] sh]# 

示例2、“ . ” 匹配除了換行符外任意一個字元

》grep "s..d" test_rules.txt

#"s..d" 會匹配在s和d這兩個字母之間一定有兩個字元的單詞

》grep "s.*d" test_rules.txt

#匹配在s和d字母之間有任意字元

》grep ".*" test_rules.txt

#匹配所有內容

[[email protected] sh]# grep "s..d" zhengzetest 
Mr. XiaoxiaoZhou said:
Later,Mr. XiaoxiaoZhou sold his hot body.
[[email protected] sh]# grep "s.*d" zhengzetest 
Mr. XiaoxiaoZhou said:
he never saaaid those words.
Later,Mr. XiaoxiaoZhou sold his hot body.
[[email protected] 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.


[[email protected] sh]# 

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

》grep "^M" zhengzetest

#匹配以大寫“M” 開頭的行

》grep “n$”  zhengzetest

#匹配以小寫“n”結尾的行

》grep -n "^$" zhengzetest

#會匹配空白行

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

示例4、“[  ]”匹配中括號中指定的任意一個字元,只匹配一個字元

》grep "s[ao]id" zhengzetest

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

》grep“[0-9]” zhengzetest

#匹配任意一個數字

》grep "^[a-z]" zhengzetest

#匹配用小寫字母開頭的行

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

示例5、“[^ ]”匹配除中括號內的字元以外的任意一個字元

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

#匹配不用小寫字母開頭的行

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

#匹配不用字母開頭的行

[[email protected] 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.
[[email protected] sh]# grep "^[^a-zA-Z" zhengzetest 
grep:  不匹配的 [ 或 [^
[[email protected] sh]# grep "^[^a-zA-Z]" zhengzetest 
123 despise him.
5555nice!
[[email protected] sh]# 

示例6、“ \”轉義符

》grep "\.$" zhengzetest     

#匹配使用“ . ” 結尾的行   因為. 在正則表示式或者說shell中有特殊含義,想要使用

其最普通的英文句號的功能,只能用轉義符將其在shell中的特殊含義去掉,再用

[[email protected] 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.
[[email protected] sh]# 

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

》grep "a\{3\}" zhengzetest

#匹配a字母連續出現三次的字串

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

#匹配包含連續的三個數字的字串

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

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

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

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

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