Emacs正则表达式:what \<和 \>\b 能做到 \b 不能做到的吗?和>
Regexp 反斜杠 - GNU Emacs 手册 说 < code>\< 匹配单词的开头,\>
匹配单词的结尾,\b
匹配单词边界。 \b
与其他非 Emacs 正则表达式一样。但似乎 \<
和 \>
是 Emacs 正则表达式特有的。是否存在需要 \<
和 \>
而不是 \b
的情况?例如,\bword\b
与 \
的匹配结果相同,唯一的区别是后者更具可读性。
Regexp Backslash - GNU Emacs Manual says that \<
matches at the beginning of a word, \>
matches at the end of a word, and \b
matches a word boundary. \b
is just as in other non-Emacs regular expressions. But it seems that \<
and \>
are particular to Emacs regular expressions. Are there cases where \<
and \>
are needed instead of \b
? For instance, \bword\b
would match the same as \<word\>
would, and the only difference is that the latter is more readable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您假设它们的行为相同,您可能会得到意想不到的结果..
什么可以\<和> \b 可以做什么?
答案是
\<
和\>
是显式...一句话就结束了!也仅此而已!\b
是通用....单词的任一结尾都将匹配...GNU 运算符 * 单词运算符
输出
You can get unexpected results if you assume they behave the same..
What can \< and > that \b can do?
The answer is that
\<
and\>
are explicit... This end of a word! and only this end!\b
is general.... Either end of a word will match...GNU Operators * Word Operators
output
在我看来,
\<.*?\>
只会匹配一系列单词字符,而\b.*?\b
将匹配任一单词系列字符或一系列非单词字符,因为它也可以接受单词的结尾,以及单词的开头。如果你强迫两者之间的表达是一个词,它们的作用确实是一样的。当然,您可以使用
\b\w
和\w\b 复制
。所以我想答案是肯定的,这主要是为了可读性。话又说回来,这不是正则表达式中大多数转义字符的用途吗?\<
和\>
的行为It looks to me like
\<.*?\>
would match only series of word characters, while\b.*?\b
would match either series of word characters or a series non-word characters, since it can also accept the end of a word, and then the beginning of one. If you force the expression between the two to be a word, they do indeed act the same.Of course, you could replicate the behavior of
\<
and\>
with\b\w
and\w\b
. So I guess the answer is that yes, it's mostly for readability. Then again, isn't that what most escape characters in regular expression are for?