linux下sed扩展正则,在sed中使用扩展正则表达式(ERE)选项时,为什...

在扩展正则表达式(ERE)中,即-r下,$表示行的结尾:

$echo '$foo' | sed -r 's/foo$/bar/'

$bar

如果您希望它表示其他含义,则必须对其进行转义:

$echo '$foo' | sed -r 's/[$]foo$/bar/'

bar

文献资料

man 7 regex解释说,在扩展正则表达式(ERE)中,$与行末匹配:

‘$’ (matching the null string at the end of a line)

相同的手册页继续说明,在没有-r的情况下得到的基本正则表达式(BRE)中,其含义更为复杂:

Obsolete (“basic”) regular expressions differ in several respects.

‘|’, ‘+’, and ‘?’ are ordinary characters and there is no

equivalent for their functionality. The delimiters for bounds are

“{” and “}”, with ‘{‘ and ‘}’ by themselves ordinary characters.

The parentheses for nested subexpressions are “(” and “)”, with ‘(‘

and ‘)’ by themselves ordinary characters. ‘^’ is an ordinary

character except at the beginning of the RE or(!) the beginning of a

parenthesized subexpression, ‘$’ is an ordinary character except at

the end of the RE or(!) the end of a parenthesized subexpression, and

‘*’ is an ordinary character if it appears at the beginning of the

RE or the beginning of a parenthesized subexpression (after a possible

leading ‘^’).

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值