正则符号

 

基础正则

 ^

* 以什么开头

```

[root@wenhaha wenhaha]# cat wenhaha1.txt

123343 3423543 45345 345

wenhaha wenhaha wehan wehshdf

 

-bash: ech: command not found

[root@wenhaha wenhaha]# grep '^[0-9]' wenhaha1.txt

123343 3423543 45345 345

```

![](_v_images/20200316212020440_16128.png)

$

* 以什么结尾

```

[root@wenhaha wenhaha]# grep '[0-9]$' wenhaha1.txt

123343 3423543 45345 345

```

![](_v_images/20200316212158059_12744.png)

 .

* 任意一个字符且只有一个字符

```

[root@wenhaha wenhaha]# grep 'w.' wenhaha1.txt

wenhaha wenhaha wehan wehshdf

```

 

 

*

* 0个及0个以上的字符

```

[root@wenhaha wenhaha]# grep 'h*' wenhaha1.txt

123343 3423543 45345 345

wenhaha wenhaha wehan wehshdf

-bash: ech: command not found

```

[]

 

* 括号内的多个字符拆分匹配

[root@wenhaha wenhaha]# grep [h,m] wenhaha1.txt
whaha whaha wehan wehshdf
-bash: ech: command not found

 

 

 [^]

* 排除括号内的字符

[root@wenhaha wenhaha]# grep [^0-9] wenhaha1.txt
123343 3423543 45345 345
whaha whaha wehan wehshdf
-bash: ech: command not found

 

 

 

 

 |

* 表示或可以匹配多个字符

[root@wenhaha wenhaha]# egrep -v '^#|^$' /etc/selinux/config 
SELINUX=enforcing
SELINUXTYPE=targeted

 

 +

* 表示连续出现一次及以上

[root@wenhaha wenhaha]# egrep [0-9]+ wenhaha1.txt
123343 3423543 45345 345

()

* 括号内的字符整合

 

 

[root@wenhaha wenhaha]# ip a s eth0 |sed -rn '3s# .* (.*)/24.*$#\1#gp'
192.168.1.223

 

 

 {}

* 指定匹配前一个字符连续出现了多次

[root@wenhaha wenhaha]# ip a s eth0 | egrep '([0-9]+.){3}[0-9]{3}' -o | head -1
192.168.1.223

 

posted @ 2020-03-17 18:31  啧啧该有就得有  阅读(130)  评论(0)    收藏  举报