加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式 – 行结束char($)在方括号内不起作用

发布时间:2020-12-14 06:02:18 所属栏目:百科 来源:网络整理
导读:将$放在方括号内对grep不起作用. ~ $echo -e "hellonthere" example.txt~ $grep "hello$" example.txt hello~ $grep "hello[$]" example.txt ~ $ 这是grep中的错误还是我做错了什么? 解决方法 这就是应该做的. [$] …定义匹配一个字符$的字符类. 因此,这将
将$放在方括号内对grep不起作用.

~ $echo -e "hellonthere" > example.txt
~ $grep "hello$" example.txt 
hello
~ $grep "hello[$]" example.txt 
~ $

这是grep中的错误还是我做错了什么?

解决方法

这就是应该做的.

[$]

…定义匹配一个字符$的字符类.

因此,这将匹配包含hello $的行.

有关正式规范,请参见the POSIX RE Bracket Expression definition.引用完整定义:

A bracket expression (an expression enclosed in square brackets,“[]” ) is an RE that shall match a single collating element contained in the non-empty set of collating elements represented by the bracket expression.

因此,任何括号表达式都匹配单个元素.

而且,在the BRE Anchoring Expression definition:年

  1. A dollar sign ( ‘$’ ) shall be an anchor when used as the last character of an entire BRE. The implementation may treat a dollar sign as an anchor when used as the last character of a subexpression. The dollar sign shall anchor the expression (or optionally subexpression) to the end of the string being matched; the dollar sign can be said to match the end-of-string following the last character.

因此 – 从BRE开始,grep默认识别的regexp格式没有参数 – 如果$不在表达式的末尾,则不需要将其识别为锚.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读