|
|
|
|
<h1><center>shell正则</center></h1>
|
|
|
|
|
|
|
|
|
|
作者:行癫(盗版必究)
|
|
|
|
|
|
|
|
|
|
------
|
|
|
|
|
|
|
|
|
|
## 一:正则
|
|
|
|
|
|
|
|
|
|
#### 1.正则介绍
|
|
|
|
|
|
|
|
|
|
正则表达式(regular expression, RE)是一种字符模式,用于在查找过程中匹配指定的字符
|
|
|
|
|
|
|
|
|
|
在大多数程序里,正则表达式都被置于两个正斜杠之间;例如/l[oO]ve/就是由正斜杠界定的正则表达式
|
|
|
|
|
|
|
|
|
|
它将匹配被查找的行中任何位置出现的相同模式。在正则表达式中,元字符是最重要的概念
|
|
|
|
|
|
|
|
|
|
重要的文本处理工具:vim、sed、awk、grep
|
|
|
|
|
|
|
|
|
|
重要的应用软件:mysql、oracle、php、python、Apache、Nginx ...
|
|
|
|
|
|
|
|
|
|
案例:
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
匹配数字: ^[0-9]+$ 123 456 +表示前面的内容出现多次
|
|
|
|
|
匹配Mail: [a-z0-9_]+@[a-z0-9]+\.[a-z]+ xingdian131420@126.com
|
|
|
|
|
匹配IP: [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3} {1,3}数字出现1-3次
|
|
|
|
|
[root@xingdian ~]# egrep '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' /etc/sysconfig/network-scripts/ifcfg-eth0
|
|
|
|
|
IPADDR=172.16.100.1
|
|
|
|
|
NETMASK=255.255.255.0
|
|
|
|
|
GATEWAY=172.16.100.25
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 2.元字符
|
|
|
|
|
|
|
|
|
|
元字符是这样一类字符,它们表达的是不同于字面本身的含义
|
|
|
|
|
|
|
|
|
|
shell元字符(也称为通配符)
|
|
|
|
|
|
|
|
|
|
正则表达式元字符
|
|
|
|
|
|
|
|
|
|
案例:
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
[root@xingdian ~]# rm -rf *.pdf
|
|
|
|
|
|
|
|
|
|
[root@xingdian ~]# grep 'abc*' /etc/passwd
|
|
|
|
|
abrt:x:173:173::/etc/abrt:/sbin/nologin
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 3.正则表达式元字符
|
|
|
|
|
|
|
|
|
|
基本正则表达式元字符:
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
元字符 功能 示例
|
|
|
|
|
^ 行首定位符 ^love
|
|
|
|
|
$ 行尾定位符 love$
|
|
|
|
|
. 匹配单个字符 l..e
|
|
|
|
|
* 匹配前导符0到多次 ab*love
|
|
|
|
|
.* 任意多个字符
|
|
|
|
|
[] 匹配指定范围内的一个字符 [lL]ove
|
|
|
|
|
[ - ] 匹配指定范围内的一个字符 [a-z0-9]ove
|
|
|
|
|
[^] 匹配不在指定组内的字符 [^a-z0-9]ove
|
|
|
|
|
\ 用来转义元字符 love\.
|
|
|
|
|
\< 词首定位符 \<love
|
|
|
|
|
\> 词尾定位符 love\>
|
|
|
|
|
\(..\) 匹配稍后使用的字符的标签 :% s/172.16.130.1/172.16.130.5/
|
|
|
|
|
:% s/\(172.16.130.\)1/\15/
|
|
|
|
|
:% s/\(172.\)\(16.\)\(130.\)1/\1\2\35/
|
|
|
|
|
:3,9 s/\(.*\)/#\1/
|
|
|
|
|
|
|
|
|
|
x\{m\} 字符x重复出现m次 o\{5\}
|
|
|
|
|
x\{m,\} 字符x重复出现m次以上 o\{5,\}
|
|
|
|
|
x\{m,n\} 字符x重复出现m到n次 o\{5,10\}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
扩展正则表达式元字符:
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
+ 匹配一个或多个前导字符 [a-z]+ove
|
|
|
|
|
? 匹配零个或一个前导字符 lo?ve
|
|
|
|
|
a|b 匹配a或b love|hate
|
|
|
|
|
() 组字符 loveable|rs love(able|rs) ov+ (ov)+
|
|
|
|
|
(..)(..)\1\2 标签匹配字符 (love)able\1er
|
|
|
|
|
x{m} 字符x重复m次 o{5}
|
|
|
|
|
x{m,} 字符x重复至少m次 o{5,}
|
|
|
|
|
x{m,n} 字符x重复m到n次 o{5,10}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 4.正则匹配示例
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
/love/
|
|
|
|
|
/^love/
|
|
|
|
|
/love$/
|
|
|
|
|
/l.ve/
|
|
|
|
|
/lo*ve/
|
|
|
|
|
/[Ll]ove/
|
|
|
|
|
/love[a-z]/
|
|
|
|
|
/love[^a-zA-Z0-9]/
|
|
|
|
|
|
|
|
|
|
/.*/
|
|
|
|
|
/^$/
|
|
|
|
|
/^[A-Z]..$/
|
|
|
|
|
/^[A-Z][a-z ]*3[0-5]/
|
|
|
|
|
/[a-z]*\./
|
|
|
|
|
/^[A-Z][a-z][a-z]$/
|
|
|
|
|
/^[A-Za-z]*[^,][A-Za-z]*$/
|
|
|
|
|
/\<fourth\>/
|
|
|
|
|
/\<f.*th\>/
|
|
|
|
|
/5{2}2{3}\./
|
|
|
|
|
5{2}
|
|
|
|
|
空行
|
|
|
|
|
/^$/
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
#### 5.正则案例
|
|
|
|
|
|
|
|
|
|
```shell
|
|
|
|
|
#!/bin/bash
|
|
|
|
|
read -p "please input number:" num
|
|
|
|
|
if [[ ! "$num" =~ ^[0-9]+$ ]]
|
|
|
|
|
then
|
|
|
|
|
echo "error number!"
|
|
|
|
|
else
|
|
|
|
|
echo "is number!"
|
|
|
|
|
fi
|
|
|
|
|
```
|
|
|
|
|
|