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