3.1.2 字符匹配
学习目标
这一节,我们从 基础知识、简单实践、小结 三个方面来学习
基础知识
简介
正则存在的根本就是对数据的匹配,而数据基本上都是有字符组成的,而正则表达式提供了非常多的字符匹配表达式,常见的表达式模式有:
单字符匹配. 匹配任意单个字符,当然包括汉字的匹配[] 匹配指定范围内的任意单个字符- 示例:[shuji]、[0-9]、[a-z]、[a-zA-Z][^] 匹配指定范围外的任意单个字符- 示例:[^shuji] | 匹配管道符左侧或者右侧的内容
简单实践
准备配置文件
[root@localhost ~]# cat keepalived.conf
! Configuration File for keepalivedglobal_defs {router_id kpmaster
}vrrp_instance VI_1 {state MASTERinterface ens33virtual_router_id 50nopreemptpriority 100advert_int 1virtual_ipaddress {192.168.8.100}
}
实践1-单字符过滤
.过滤单个字符
[root@localhost ~]# grep 'st..e' keepalived.conf
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
[root@localhost ~]# grep 'ens..' keepalived.confinterface ens33
实践2-范围单字符过滤
[] 过滤范围字符
[root@localhost ~]# grep 'i[a-z]t' keepalived.confinterface ens33virtual_router_id 50advert_int 1virtual_ipaddress {
[root@localhost ~]# grep 'i[a-n]t' keepalived.confinterface ens33advert_int 1
[root@localhost ~]# grep '[b-c]' keepalived.conf
global_defs {
vrrp_instance VI_1 {interface ens33
[root@localhost ~]# egrep '[x-z]' keepalived.confpriority 100
实践3-反向单字符过滤
只要包括的内容,都不要显示
[root@localhost ~]# grep '[^a-Z_ }{0-5]' keepalived.conf
! Configuration File for keepalived192.168.8.100
实践4-过滤特定的字符范围
[root@localhost ~]# egrep 'state|priority' keepalived.confstate MASTERpriority 100[root@localhost ~]# egrep 'st|pri' keepalived.confrouter_id kpmaster
vrrp_instance VI_1 {state MASTERpriority 100
小结