一、定义:元字符字符是这样一类字符,它们表达的是不同字面本身的含义
二、分类:
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# $ 行尾定位
[root@localhost ~]# grep love$ 1.txt
love# . 匹配任意单个字符
[root@localhost ~]# grep a.c 1.txt
abc
adc# * 匹配前导符0次到多次
[root@localhost ~]# grep "abcde*" 1.txt
abcd
abcde
abcdef
abcdefj# .* 任意多个字符
[root@localhost ~]# grep ".*" 1.txt
qian
yoa
huang
kai
kong# [] 匹配指定范围内的一个字符
[root@localhost ~]# grep "[lL]ove" 1.txt
love
love
Love# [-] 匹配指定范围内的一个字符,连续的范围[root@localhost ~]# grep "[a-z]ove" 1.txt
love
love# [^] 匹配不在指定组内的字符
[root@localhost ~]# grep "[^lL]ove" 1.txt
1ove
|ove# \ 用来转义元字符('' "" \),脱意符
[root@localhost ~]# grep "l\.ve" 1.txt
l.ve# \< 词首定位符
[root@localhost ~]# grep "\<love" 1.txt
love
love# \> 词尾定位符[root@localhost ~]# grep "love\>" 1.txt
love
love# () 匹配稍后使用的字符的标签
:1,2 s/(.*\)/#\1/ #加注释# x\{m\} 字符x重复出现m次
[root@localhost ~]# grep "o\{3\}" 1.txt
|oooove# x\{m,\} 字符x重复出现m次以上
[root@localhost ~]# grep "o\{3,\}" 1.txt
|oooove
looove
loooooooove# x\{m,n\} 字符x重复出现m到n次
[root@localhost ~]# grep "o\{3,4\}" 1.txt
|oooove
looove
loooooooove
2、扩展正则表达式元字符
# + 匹配1-n个前导字符
[root@localhost ~]# egrep lo+ve 1.txt
love
love
looove
loooooooove# ? 匹配0-1个前导字符
[root@localhost ~]# egrep lo?ve 1.txt
love
love# a | b 匹配a或b
[root@localhost ~]# egrep "o|v" 1.txt
yoa
kong
duo
gou
love
love# () 组字符
[root@localhost ~]# egrep "love(able|rs)" 1.txt
loveable
lovers[root@localhost ~]# egrep "loveable|rs" 1.txt
loveable
lovers