元字元 | 作 用 |
---|---|
* | 前一個字元匹配 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 位的小寫字母 |
[[email protected] ~】# vi /root/.bashrc
alias grep='grep --color=auto'
[[email protected] ~]#vi test_rule.txt
Mr. Li Ming said:
he was the most honest man in LampBrother.
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.
[[email protected] ~]# grep "a*" test_rule.txt
Mr. Li Ming said:
he was the most honest man in LampBrother.
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.
[[email protected] ~]# grep "aa*" test_rule.txt Mr. Li Ming said:
he was the most honest man in LampBrother. 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
[[email protected] ~]# grep "aaa*" test_rule.txt
he never saaaid those words.
because,actuaaaally,
[[email protected] ~]# grep "aaaaa*" test_rule.txt
because,actuaaaally,
[[email protected] ~]# grep "s..d" test_rule.txt
Mr. Li Ming said:
Later,Mr. Li ming soid his hot body.
# "s..d"會匹配在s和d這兩個字母之間一定有兩個字元的單詞
[[email protected] ~]# 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"
[[email protected] ~]# grep ".*" test_rule.txt
Mr. Li Ming said:
he was the most honest man in LampBrother.
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
[[email protected] ~]# grep "^M" test_rule.txt
Mr. Li Ming said:
Mr. Shen Chao is the most honest man
[[email protected] ~]# grep "n$" test_rule.txt
Mr. Shen Chao is the most honest man
[[email protected] ~]# grep -n "^$" test_rule.txt
4:
8:
11:
[[email protected] ~]# grep "s[ao]id" test_rule.txt
Mr. Li Ming said:
Later,Mr. Li ming soid his hot body.
而"[0-9]"會匹配任意一個數位,例如:
[[email protected] ~]# grep "[0-9]" test_rule.txt 123despise him.
5555nice!
#列出包含有數位的行
[[email protected] ~]# grep "[A-Z|" test_rule.txt
Mr. Li Ming said:
he was the most honest man in LampBrother.
But since Mr. shen Chao came,
Mr. Shen Chao is the most honest man
Later,Mr. Li ming soid his hot body.
#列出包含大寫字母的行
[[email protected] ~]# grep "^[a-z]" test_rule.txt
he was the most honest man in LampBrother.
he never saaaid those words.
because,actuaaaally,
[[email protected] ~]# grep "^[^a-z]" test_rule.txt
Mr. Li Ming said:
123despise him.
But since Mr. shen Chao came,
5555nice!
Mr. Shen Chao is the most honest man Later,Mr. Li ming soid his hot body.
[[email protected] ~]# grep "^[^a-zA-Z]" test_rule.txt 123despise him.
5555nice!
[[email protected] ~]# grep ".$" test_rule.txt
he was the most honest man in LampBrother.
123despise him.
he never saaaid those words.
Later,Mr. Li ming soid his hot body.
[[email protected] ~]# grep "a{3}" test_rule.txt
he never saaaid those words,
because,actuaaaally,
[[email protected] ~]# grep "[su]a{3}[il]" test_rule.txt
he never saaaid those words.
#只匹配3個連續的a
[[email protected] ~]# grep "[su]a{4}[il]" test_rule.txt because,actuaaaally,
# 只匹配4個連續的a
[[email protected] ~]# grep "[0-9]{3}" test_rule.txt
123despise him.
5555nice!
[[email protected] -]# grep "^[0-9]{3}[a-z]" test_mle.txt
123despise him.
#只匹配以連續三個數位開頭的行
[[email protected] ~]# grep "^[0-9]{4}[a-z]"test_mle.txt
5555nice!
#只匹配以連續四個數位開頭的行
[[email protected] ~]# grep "^[0-9]{3,}[a-z]" test_rule.txt
123despise him.
5555nice!
#匹配最少以連續三個數位開頭的行
[[email protected] ~]# grep "[su]a{3,}[il]" test_mle.txt
he never saaaid those words, because,actuaaaaly,
#匹配在字母 s 或 u 和 i 或 l 之間最少出現 3 個連續的 a 的字串
[[email protected] ~]# grep "sa{1,3}i" test_rule.txt
Mr. Li Ming said:
he never saaaid those words.
#匹配在字母s和字母i之間最少有一個a、最多有3個a的字串
[[email protected] ~]# grep "sa{2,3}i" test_rule.txt
he never saaaid those words.
#匹配在字母s和字母i之間最少有兩個a、最多有3個a的字串
擴充套件元字元 | 作 用 |
---|---|
+ |
前一個字元匹配 1 次或任意多次。 如“go+gle”會匹配“gogle” “google”或“gooogle”。當然,如果“o”有更多個,則也能匹配 |
? |
前一個字元匹配 0 次或 1 次。 如 “colou?r” 可以匹配 “colour” 或 “color” |
| |
匹配兩個或多個分支選擇。 如“was|his”既會匹配包含“was”的行,也會匹配包含“his”的行 |
() |
匹配其整體為一個字元,即模式單元。可以理解為由多個單個字元組成的大字元。 如“(dog)+”會匹配“dog” “dogdog” “dogdogdog”等,因為被()包含的字元會被當成一個整體。但 “hello(world|earth)” 會匹配 “hello world” 及 “hello earth” |