正则表达式(regular expression, RE)是一种字符模式,用于在查找过程中匹配指定的字符。
在大多数程序里,正则表达式都被置于两个正斜杠之间;例如/l[oO]ve/就是由正斜杠界定的正则表达式
它将匹配被查找的行中任何位置出现的相同模式。在正则表达式中,元字符是最重要的概念。
被vim、sed、awk、grep调用
mysql、oracle、php、python ,Apache,Nginx... 需要正则
示例 需求 匹配数字的脚本:用户输入创建账号的数量 语法 [[ ^[0-9]+$ ]] 示范 read -p "输入数字才退出: " num while : do if [[ ! $num =~ ^[0-9]+$ ]];then echo "error enter!" read -p "输入数字才退出:" num else echo "thank you" exit 1 fi done
定义:元字符是这样一类字符,它们表达的是不同于字面本身的含义
^ 行首定位符
[root@localhost ~]# grep "root" /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@localhost ~]# grep "^root" /etc/passwd 为什么什么少一行? root:x:0:0:root:/root:/bin/bash
$ 行尾定位符
love$
. 匹配任意单个字符
[root@localhost ~]# grep abc 1.txt abc [root@localhost ~]# grep adc 1.txt adc [root@localhost ~]# grep a.c 1.txt abc adc
* 匹配前导符0到多次
[root@localhost ~]# cat 1.txt a ab abc abcd abcde abcdef ggg hhh iii [root@localhost ~]# grep "abc*" 1.txt ab abc abcd abcde abcdef [root@localhost ~]# grep "abcd*" 1.txt abc abcd abcde abcdef
.* 任意多个字符
[root@localhost ~]# grep ".*" 1.txt a ab abc abcd abcde abcdef ggg hhh iii
[ ] 匹配指定范围内的一个字符
[lL]ove [root@localhost ~]# cat 1.txt love Love 1ove iove [root@localhost ~]# grep [lL]ove 1.txt love Love
[ - ] 匹配指定范围内的一个字符,连续的范围
[a-z0-9]ove //////[a-Z]=[a-zA-Z] [root@localhost ~]# cat 1.txt love Love 1ove iove [root@localhost ~]# grep [0-9] 1.txt 1ove [root@localhost ~]# grep [a-z]ove 1.txt love iove [root@localhost ~]# grep [a-Z]ove 1.txt love Love iove
[^] 匹配不在指定组内的字符
[^a-z0-9]ove //////取反 [root@localhost ~]# cat 1.txt love Love 1ove iove [root@localhost ~]# grep "[^a-z]ove" 1.txt Love 1ove [root@localhost ~]# grep "[^0-9]ove" 1.txt love Love iove
\ 用来转义元字符 ('' "" \),脱意符
[root@localhost ~]# grep "l." 1.txt love l.ve [root@localhost ~]# grep "l\." 1.txt l.ve
\< 词首定位符
[root@localhost ~]# grep "love" 1.txt love iloveyou [root@localhost ~]# grep "\<love" 1.txt love
^ 和 \<的区别
[root@localhost ~]# cat 1.txt i loveyou loveyou [root@localhost ~]# grep "\<love" 1.txt i loveyou loveyou [root@localhost ~]# grep "^love" 1.txt loveyou
\> 词尾定位符
love\>
() \(..\) 匹配稍后使用的字符的标签
:3,9 s/\(.*\)/#\1/ 加注释
x\{m\} 字符x重复出现m次
[root@localhost ~]# grep o 1.txt love loove looove [root@localhost ~]# grep "o\{3\}" 1.txt looove
x\{m,\} 字符x重复出现m次以上
o\{5,\}
x\{m,n\} 字符x重复出现m到n次
o\{5,10\} [root@localhost ~]# egrep "o{4,5}" 1.txt oooo ooooo ioooo ooooi iooooi [root@localhost ~]# egrep "o{5,5}" 1.txt ooooo
+ 匹配1~n个前导字符
[root@localhost ~]# cat 1.txt lve love loove [root@localhost ~]# egrep lo+ve 1.txt love loove
? 匹配0~1个前导字符
[root@localhost ~]# egrep lo?ve tom.sh love lve
a|b 匹配a或b
[root@localhost ~]# egrep "o|v" 1.txt lve 1ove loove looove loeve love Love iloveyou l.ve o oo ooo oooo ooooo ioooo ooooi iooooi
() 组字符
[root@localhost ~]# egrep "loveable|rs" 1.txt rs loveable lovers [root@localhost ~]# egrep "love(able|rs)" 1.txt loveable lovers