sed 和 grep 中单词的开头和结尾 [英] Beginning and end of words in sed and grep

查看:104
本文介绍了sed 和 grep 中单词的开头和结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白 GNU sed 和 GNU grep 中 \b\< 之间的区别.在我看来 \b 总是可以替换 \<\\> 而不改变匹配的字符串集.

I don't understand the difference between \b and \< in GNU sed and GNU grep. It seems to me \b can always replace \< and \\> without changing the set of matching strings.

更具体地说,我试图找到 \bsomething\\< 的示例.东西不匹配完全相同的字符串.

More specifically, I am trying to find examples in which \bsomething and \\< something do not match exactly the same strings.

something\bsomething\\> 的相同问题.

谢谢

推荐答案

我怀疑无论您使用(更常见的)\b 还是(更具体的)\<\>,但我可以想到一个例子.这是相当人为的,我怀疑在大多数现实世界的正则表达式使用中它不会有什么不同,但这应该表明它至少在某些情况下可以有所作为.

I suspect that it very rarely makes a difference whether you use (the more common) \b or (the more specific) \< and \>, but I can think of an example where it would. This is quite contrived, and I suspect that in most real-world regex use it wouldn't make a difference, but this should demonstrate that it at least could make a difference in some cases.

如果我有以下文字:

this is his pig

我想知道 /\bis\b/ 是否匹配,如果我改为使用 /\<is\>/ 或我改为使用 /\>is\</

and I want to know if /\bis\b/ matches, it wouldn't matter if I instead used /\<is\>/ or I instead used /\>is\</

但如果我的文字改为

is this his pig

在is"之前不再有词尾边界,只有词首边界.使用 /\bis\b/ 匹配,当然 /\<is\>/ 也可以,但是 /\>is\</ 没有.

There's no longer a word-final boundary before the 'is', only a word-initial boundary. Using /\bis\b/ matches, and of course /\<is\>/ does too, but /\>is\</ does not.

然而,在现实生活中,我认为你真的需要能够做出这种区分并不常见,这就是为什么(至少在 sed 之外)\b 是正常词正则表达式的边界标记.

In real life, though, I think it is not common that you really need to be able to make this distinction, which is why (at least outside of sed) \b is the normal word boundary marker for regular expressions.

这篇关于sed 和 grep 中单词的开头和结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆