1.1、名词解释
正则表达式(regular expression, RE)是一种字符模式,用于在查找过程中匹配指定的字符。
在大多数程序里,正则表达式都被置于两个正斜杠之间;例如/l[oO]ve/就是由正斜杠界定的正则表达式
它将匹配被查找的行中任何位置出现的相同模式。在正则表达式中,元字符是最重要的概念。
1.2、工具
被vim、sed、awk、grep调用
1.3、场景
mysql、oracle、php、python ,Apache,Nginx… 需要正则
1.4、回顾
示例
需求
匹配数字的脚本:用户输入创建账号的数量
语法
[[ ^[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
定义:元字符是这样一类字符,它们表达的是不同于字面本身的含义
2.1、基本正则表达式元字符
^行首定位符
[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< code></love">
^ 和 \
Original: https://www.cnblogs.com/Jqazc/p/16655234.html
Author: 我真的兔了
Title: Shell第四章《正则表达式》
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/578411/
转载文章受原作者版权保护。转载请注明原作者出处!