正则表达式(regular expression, RE)是一种字符模式(pattern),用于在查找过程中匹配指定的字符。
在大多数程序里,正则表达式都被置于两个正斜杠之间;例如/l[oO]ve/就是由正斜杠界定的
正则表达式,
它将匹配被查找的行中任何位置出现的相同模式。在正则表达式中,元字符是最重要的概念。
^ 行首定位符 ^love
[root@shell ~]# [[ "$num2" =~ ^[0-9]+$ ]] && echo "number" ||echo "not number"
not number
$ 行尾定位符 love$
[root@shell ~]# [[ "$num2" =~ ^[0-9]+$ ]] && echo "number" ||echo "not number"
not number
. 匹配单个字符 l..e
[root@shell ~]# grep 'r..t' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
* 匹配前导符 0 到多次 ab*love
[root@shell ~]# grep --color 'r*ot' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
.* 任意多个字符
[root@shell ~]# grep --color 'roo*t' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[ ] 匹配指定范围内的一个字符 [lL]ove
[root@shell ~]# grep '[Rr]oot' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[ - ] 匹配指定范围内的一个字符 [a-z0-9]ove
[root@shell ~]# grep '[a-z]oot' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[^] 匹配不在指定组内的字符 [^a-z0-9]ove
[root@shell ~]# grep '[^0-9A-Z]oot' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
\ 用来转义元字符 love\.
[root@shell ~]# grep '\/:' /etc/passwd
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
\< 词首定位符 \
[root@shell ~]# grep '\<root\>' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
\> 词尾定位符 love\>
[root@shell ~]# grep '\<root\>' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
\(..\) 匹配稍后使用的字符的标签
vim置换
:% s/172.16.130.1/172.16.130.5/
:% s#\(192.\)\(168.\)\(40.\)100#\1\2\3200#
:% s/\(172.16.130.\)1/\15/
:3,9 s/\(.*\)/#\1/ -------查找3到9行加上注释
x\{m\} 字符 x 重复出现 m 次 o\{5\}
[root@shell ~]# egrep 'ro{2}' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
x\{m,\} 字符 x 重复出现 m 次以上 o\{5,\}
[root@shell ~]# egrep 'ro{1,}' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
x\{m,n\} 字符 x 重复出现 m 到 n 次 o\{5,10\}
[root@shell ~]# egrep 'ro{1,2}' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
+ 匹配一个或多个前导字符 [a-z]+ove
[root@shell ~]# egrep 'ro+t' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
? 匹配零个或一个前导字符 lo?ve
[root@shell ~]# egrep 'r?ot' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
a|b 匹配 a 或 b love|hate
[root@shell ~]# egrep 'root|halt' /etc/passwd
root:x:0:0:root:/root:/bin/bash
halt:x:7:0:halt:/sbin:/sbin/halt
operator:x:11:0:operator:/root:/sbin/nologin
() 组字符 loveable|rs love(able|rs) ov+ (ov)+
[root@shell ~]# egrep 'r(oo|oot)' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
(..)(..)\1\2 标签匹配字符 (love)able\1er
x{m} 字符 x 重复 m 次 o{5}
x{m,} 字符 x 重复至少 m 次 o{5,}
x{m,n} 字符 x 重复 m 到 n 次 o{5,10}
root* 0-n
root? 0-1
root+ 1-n
root{2} 2
ro(ot)*
ro(ot)?
ro(ot)+
ro(ot){2}
POSIX 字符类:
表达式 功能 示例
[:alnum:] 字母与数字字符 [[:alnum:]]+
[:alpha:] 字母字符(包括大小写字母) [[:alpha:]]{4}
[:blank:] 空格与制表符 [[:blank:]]*
[:digit:] 数字字母 [[:digit:]]?
[:lower:] 小写字母 [[:lower:]]{5,}
[:upper:] 大写字母 [[:upper:]]+
[:punct:] 标点符号 [[:punct:]]
[:space:] 换行符,回车等在内的所有空白[[:space:]]+