码迷,mamicode.com
首页 > 其他好文 > 详细

正则符号

时间:2020-03-17 19:35:13      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:-bash   targe   inf   ted   有一个   ima   sel   grep   基础   

 

基础正则

 ^

* 以什么开头

```

[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

技术图片

 

正则符号

标签:-bash   targe   inf   ted   有一个   ima   sel   grep   基础   

原文地址:https://www.cnblogs.com/wenhaha/p/12512571.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!