脚本编程大全:第二十章 正则表达式

Linux 中的两种主流扩展正则表达式引擎
basic regular expression BRE
extended regular expressiong ERE

!!!!在匹配过程中,空格与其他字符并无区别!!!!
匹配过程中的转义

┌─[root@parrot]─[/home/user/Desktop/playground]
└──╼ #echo "This is cost \$4.22" | sed -n '/\$/p'
This is cost $4.22

┌─[root@parrot]─[/home/user/Desktop/playground]
└──╼ #echo "This is a \ a" | sed -n  '/\\/p'
This is a \ a

是否在行首  
┌─[root@parrot]─[/home/user/Desktop/playground]
└──╼ #echo "This is This  cost \$4.22" | sed  '/^This/p'


查看以d 或 -开头的行
└──╼ #cat lafile  |  grep   "^[d-]"


匹配五位数字
┌─[root@parrot]─[/home/user/Desktop/playground]
└──╼ #cat num
123654
12364
2495
12396
95684
┌─[root@parrot]─[/home/user/Desktop/playground]	
└──╼ #cat num  | grep "^[0-9][0-9][0-9][0-9][0-9]$"
12364
12396
95684

指定多个区间

┌─[root@parrot]─[/home/user/Desktop/playground]
└──╼ #echo "dbc" | sed -n '/[a-en-z]bc/p'
dbc
┌─[root@parrot]─[/home/user/Desktop/playground]
└──╼ #echo "ybc" | sed -n '/[a-en-z]bc/p'
ybc

在这里插入图片描述

┌─[root@parrot]─[/home/user/Desktop/playground]
└──╼ #echo "ybc" | sed -n '/[[:upper:]]/p'
┌─[root@parrot]─[/home/user/Desktop/playground]
└──╼ #echo "ybc" | sed -n '/[[:lower:]]/p'
ybc

星号 * 表示前一个字符出现0次或多次
可以看到单个的r也被匹配到了
在这里插入图片描述通过与点.的组合匹配多个任意字符
吊!

在这里插入图片描述

扩展正则表达式

? 类似*但只匹配零次或一次

+一次或多次

在这里插入图片描述

使用花括号 {m,n} 表明上一个字符至少m次,至多n次
有的awk不支持默认的间隔 需要指定如下样式
在这里插入图片描述
| 匹配选项 | 两旁不能有空格

┌─[root@parrot]─[/home/user/Desktop/playground]
└──╼ #echo "appppppd" | gawk --re-interval '/ap{2,3}d|ap{1,13}d/{print $0}'
appppppd

表达式分组 ()

在这里插入图片描述

实例:
美国电话号码 在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值