正则表达式
基础正则表达式:BRE(basic regular express)
扩展正则表达式:ERE(extend regular express),扩展的表达式有+、?、|和()
符号 | 描述 | 范例 |
. | 出 匹配任意一个单字符 |
[root@localhost ~]# echo -e "123444
xf"|grep "12." |
^ | 匹配字符串开头的 |
[root@localhost ~]# echo -e "123444
xf"|grep "^12" |
$ | 匹配前面字符串结尾 |
[root@localhost ~]# echo -e "123444
xf"|grep "f$" |
* | 匹配前面字符0个或多个 |
[root@localhost ~]# echo -e "xo
xooo
xoghjk"|grep "o*" |
+ | 匹配前面一个或多个 |
[root@localhost ~]# echo -e "abc
abbbbcc
add" |grep -E 'ab+' |
? | 匹配前字符0或1个 |
[root@localhost ~]# echo -e "ac
abbbc
addd"|grep -E "a?c" |
[] | 匹配任意一个字符 |
[root@localhost ~]# echo -e "ac
ad
db"|grep "[ac]" |
[.-.] | 匹配括号中任意字符 | [0-9] [a-z] |
[^] | 匹配除括号内的字符 |
[root@localhost ~]# echo -e "ac
ad
db"|grep "[^ac]" |
{n} | 至少匹配n次前面的字符 |
[root@localhost ~]# echo -e "a
abc
c" |grep -E '[a-z]{3,}' |
{n,m} | 匹配n次-m次 |
[root@localhost ~]# echo -e "a
abc
cd" |grep -E '[a-z]{1,3}' |
() | 单元或组合,就是一个整体 |
[root@localhost ~]# echo "123abc" |grep -E -o '(abc)' |
< | 以字符开始 |
[root@localhost ~]# echo -e "1
12
123
1234" |grep '<123' |
> | 以字符结束 |
[root@localhost ~]# echo -e "1
12
123
1234" |grep '4>' |
| | 匹配两边的任意一个 |
[root@localhost ~]# echo -e "123
abc" |grep -o -E "(123|abc)" |