一、正則表達式介紹
正則表達式是一種文本模式匹配,包括普通字符(a...z)和特殊字符(元字符)。
它是一種字符串匹配模式,可以用來檢查一個字符串是否含有某種子串、將匹配的子串替換或者從某個字符串中取出某個條件的子串
shell支持正則表達式,但是不是所有的命令都支持正則,常見的命令中只有grep、sed、awk命令支持正則表達式
二、特殊字符
1、定位符使用-模糊匹配與精准匹配:
同時錨定開頭和結尾,做精確匹配;單一錨定開發和結尾,做模糊匹配。
定位符 | 說明 |
^ | 錨定開頭^a以a開發,默認錨定一個字符 |
$ | 錨定結尾a$以a結尾,默認錨定一個字符 |
舉例說明:定位符
[root@localhost test20210731]# egrep "^abbbc$" file #正則匹配,等價於grep -e 或 grep -E,精確匹配 abbbc [root@localhost test20210731]# egrep "^ab" file #匹配開頭為ab abbbc abababa abC [root@localhost test20210731]# egrep "bb$" file #匹配結尾為bb aabb &abbb bbbb
2、匹配符-匹配字符串:
匹配符 | 說明 |
. | 匹配除回車以外的任意字符 |
() | 字符串分組 |
[] | 定義字符串,匹配括號中的一個字符 |
[^] | 表示否定括號中出現字符串的字符,取反 |
\ | 轉義字符 |
| | 管道-或,結合分組使用 |
舉例說明匹配符:
[root@localhost test20210806]# egrep "^a.c$" file #匹配a開頭,c結尾,中間任意字符 aBc aYc a*c a4c a9c a7c [root@localhost test20210801]# egrep "^a[0-9]c$" file #匹配a開頭c結尾,中間的字符為0-9 a4c a9c a7c [root@localhost test20210801]# egrep "^a[^0-9]c$" file #匹配a開頭c結尾,中間非數字 aBc aYc a*c [root@localhost test20210801]# egrep "^a\*c$" file #精確匹配a*c的情況 a*c [root@localhost test20210801]# egrep "^a*c$" file #不加轉義無法匹配 ac [root@localhost test20210801]# egrep "^(a|b)c$" file #精確匹配以a或b開頭,c結尾 ac bc
3、限定符-對前面的符合或字符串做限定說明
限定符 | 說明 |
* | 某個字符之后加星號表示該字符不出現或出現多次 |
? | 與型號類似,但略有不行,表示該字符出現一次或不出現 |
+ | 與星號類似,表示其前面字符出現一次或多次,但是至少出現一次 |
{n,m} | 某個字符之后出現,表示該字符最少n次,最多m次 |
{m} | 某個字符出現m次 |
舉例說明限定符:
[root@localhost test20210806]# egrep "^ab*c$" file #ab字符中匹配有b(全部需要是b)或沒有b abbbc ac [root@localhost test20210806]# egrep "^ab*c$" file #ab字符中匹配有b(全部需要是b)或沒有b abbbc ac abc [root@localhost test20210806]# egrep "^ab?c$" file #ab字符中匹配有b(出現一次)或沒有b ac abc [root@localhost test20210806]# egrep "^ab+c$" file #ac字符中匹配有b(至少出現一次) abbbc abc [root@localhost test20210806]# egrep "^ab*c$" file #ac字符中匹配有b(全部需要是b)或沒有b abbbc ac abc [root@localhost test20210806]# egrep "^ab?c$" file #ac字符中匹配有b(出現一次)或沒有b ac abc [root@localhost test20210806]# egrep "^ab+c$" file #ac字符中匹配有b(至少出現一次) abbbc abc [root@localhost test20210806]# egrep "^ab{1,3}c$" file #ac字符中匹配有b(出現在1次到3次內) abbbc abc [root@localhost test20210806]# egrep "^ab{3}c$" file #ac字符中匹配有b(正好出現3次) abbbc
三、POSIX字符
特殊字符 | 說明 |
[:alnum:] | 匹配任意字母字符0-9 a-z A-Z |
[:alpha:] | 匹配任意字母,大寫或小寫 |
[:dight:] | 數字0-9 |
[:graph:] | 非空字符(非空格控制字符) |
[:lower:] | 小寫字符a-z |
[:upper:] | 大寫字符A-Z |
[:cntrl:] | 控制字符 |
[:print:] | 非空字符(包括空格) |
[:punct:] | 標點符號 |
[:blank:] | 空格和TAB字符 |
[:xdigit:] | 16進制數字 |
[:space:] | 所有空白字符(新行、空格、制表符) |
注意:[[]]雙中括號的意思:第一個中括號是匹配符[]匹配中括號中的任意一個字符,第二個[]格式如[:digit:]
舉例說明:
[root@localhost tesr20210807]# egrep "^a[[:alnum:]]c$" file #a開頭c結尾,中間一個字符匹配非特殊符號 aBc aYc a4c a9c a7c abc [root@localhost tesr20210807]# egrep "^a[[:alnum:]]c$" file #a開頭c結尾,中間一個字符匹配任意字母 aBc aYc a4c a9c a7c abc [root@localhost tesr20210807]# egrep "^a[[:alnum:]]c$" file #a開頭c結尾,中間一個字符匹配非特殊符號 aBc aYc a4c a9c a7c abc [root@localhost tesr20210807]# egrep ^C]c$" file #a開頭c結尾,中間一個字符匹配任意字母
[root@localhost tesr20210807]# egrep "^a[[:alnum:]]c$" file #a開頭c結尾,中間一個字符匹配非特殊符號 aBc aYc a4c a9c a7c abc [root@localhost tesr20210807]# egrep "^a[[:alpha:]]c$" file #a開頭c結尾,中間一個字符匹配任意字母 aBc aYc abc [root@localhost tesr20210807]# egrep "^a[[:digit:]]c$" file #a開頭c結尾,中間一個字符匹配任意數字 a4c a9c a7c [root@localhost tesr20210807]# egrep "^a[[:graph:]]c$" file #a開頭c結尾,中間一個字符匹配非空字符 aBc aYc a*c a4c a9c a7c abc a,c [root@localhost tesr20210807]# egrep "^a[[:lower:]]c$" file #a開頭c結尾,中間一個字符匹配小寫字母 abc [root@localhost tesr20210807]# egrep "^a[[:upper:]]c$" file #a開頭c結尾,中間一個字符匹配大寫字母 aBc aYc [root@localhost test20210807]# egrep "^a[[:blank:]]c$" file #a開頭c結尾,中間一個字符為空格或TAB a c a c [root@localhost test20210807]# egrep "^a[[:space:]]c$" file #a開頭c結尾,中間匹配所有空白、空行、制表符 a c a c [root@localhost test20210807]# egrep "^a[[:blank:]]c$" file #a開頭c結尾,中間一個字符為空格或TAB a c a c [root@localhost test20210807]# egrep "^a[[:space:]]c$" file #a開頭c結尾,中間匹配所有空白、空行、制表符 a c a c [root@localhost test20210807]# egrep "^a[[:blank:]]c$" file #a開頭c結尾,中間一個字符為空格或TAB a c a c [root@localhost test20210807]# egrep "^a[[:space:]]c$" file #a開頭c結尾,中間一個字符匹配空白、空行、制表符 a c a c [root@localhost test20210807]# egrep "^a[[:punct:]]c$" file #a開頭c結尾,中間一個字符匹配標點符號 a*c a,c [root@localhost test20210807]# egrep "^a[[:print:]]c$" file #a開頭c結尾,中間一個字符匹配非空字符(含括號) aBc aYc a*c a4c a9c a7c abc a c a,c [root@localhost test20210807]# egrep "^a[[:xdigit:]]c$" file #a開頭c結尾,中間一個字符匹配十六進制數 aBc a4c a9c a7c abc
四、常見正則匹配:
1、數字:^[0-9]*$
2、數字或英文字母:^[A-Za-z0-9]+$
2、英文字母:^[A-Za-z]+$
3、日期格式(2021-05-01或2021-5-1):[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}
4、手機號碼:^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$
5、IP地址:^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\.
6、E-mail:^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$
7、身份證(15位或18位):(^[1-9][0-9]{5}(18|19|([23][0-9]))[0-9]{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)[0-9]{3}[0-9Xx]$)|(^[1-9][0-9]{5}[0-9]{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)[0-9]{2}$)