'git grep' Mac OS X 和 BSD 上的字边界
我在 Linux 开发服务器上定期运行 git grep "\\<
和 < code>\> 在 Mac (Mac OS X 10.6.8) 上(无法使用 = 没有找到任何内容)。 Mac 中的正则表达式语法是否不同?
我尝试使用 git grep -E "\
I run git grep "\<blah\>"
regularly on my linux development server, but I just discovered that I am not able to use \<
and \>
on Mac (Mac OS X 10.6.8) (not able to use = it does not find anything). Is the regular expressions syntax different in Mac?
I tried using git grep -E "\<blah\>"
but to no avail! :-(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在苦苦挣扎之后,我也发现了 这篇非常有用的帖子 在 BSD 邮件列表上。所以这是(尽管相当难看)的解决方案:
git-grep 的
-w
标志也可以工作,但有时您只想匹配单词的开头或结尾。更新:这在 OS X 10.9“Mavericks”中已更改。现在您可以使用
\<
、\>
和\b
。不再支持[[:<:]]
和[[:>:]]
。After struggling with this, too, I found this very helpful post on a BSD mailing list. So here's the (albeit rather ugly) solution:
The
-w
flag of git-grep also works but sometimes you want to only match the beginning or end of a word.Update: This has changed in OS X 10.9 "Mavericks". Now you can use
\<
,\>
, and\b
.[[:<:]]
and[[:>:]]
are no longer supported.我猜这是由 BSD 与 Linux grep 库引起的。
看看 git grep 的
-w
(仅在单词边界匹配模式)选项是否适合您:I guess it's caused by the BSD vs Linux grep library.
See if the
-w
(match pattern only at word boundary) option to git grep does it for you:您可以使用
PCRE
支持来编译 git,并使用git grep -P "\bblah\b"
作为字边界。以下是有关如何使用 OSX Homebrew 编译 git 的指南:
http:// /realultimateprogramming.blogspot.com/2012/01/how-to-enable-git-grep-p-on-os-x-using.html
You can compile git with
PCRE
support and usegit grep -P "\bblah\b"
for word boundaries.Here's a guide on how to compile git using OSX Homebrew:
http://realultimateprogramming.blogspot.com/2012/01/how-to-enable-git-grep-p-on-os-x-using.html
如果您确实使用
-P
,请确保使用 Git 2.40 (Q1 2023):“grep -P
” 学会了在处理 < 时使用 Unicode 字符属性来理解字符类。 code>\b 和\w
等。参见 提交 acabd20(2023 年 1 月 8 日),作者:卡洛·马塞洛·阿里纳斯·贝隆(
carenas
)。(由 Junio C Hamano --
gitster
-- 合并于 提交 557d93a,2023 年 1 月 27 日)这意味着这些模式适用于任何字符:
在 Git 2.41(2023 年第 2 季度)中,最近的一项允许 Unicode 字符类与“
grep -P
”一起使用的更改触发了 JIT bug较旧的pcre2
库。使用这些旧库构建的 Git 中存在问题的更改已被禁用,以解决该错误。
请参阅 提交 14b9a04(2023 年 3 月 23 日),作者:Mathias Krause (
mathiaskrause
)。(由 Junio C Hamano --
gitster
-- 合并于 提交 d35cd54,2023 年 3 月 30 日)Git 2.48(2025 年第 1 季度),第 7 批,修复了 '
git grep
'(man):通过在遇到无效的 UTF-8 字节序列时禁用前瞻来修复 macOS 上的回归。请参阅 commit ce025ae(2024 年 10 月 20 日),作者:René Scharfe (
rscharfe
)。(由 Taylor Blau --
ttaylorr
-- 合并于 提交 43ac239,2024 年 11 月 1 日)If you do use
-P
, make sure to use Git 2.40 (Q1 2023): "grep -P
" learned to use Unicode Character Property to grok character classes when processing\b
and\w
etc.See commit acabd20 (08 Jan 2023) by Carlo Marcelo Arenas Belón (
carenas
).(Merged by Junio C Hamano --
gitster
-- in commit 557d93a, 27 Jan 2023)That means those patterns will work, with any character:
With Git 2.41 (Q2 2023), a recent-ish change to allow Unicode character classes to be used with "
grep -P
" triggered a JIT bug in olderpcre2
libraries.The problematic change in Git built with these older libraries has been disabled to work around the bug.
See commit 14b9a04 (23 Mar 2023) by Mathias Krause (
mathiaskrause
).(Merged by Junio C Hamano --
gitster
-- in commit d35cd54, 30 Mar 2023)Git 2.48 (Q1 2025), batch 7, fixes another issue with '
git grep
'(man): a regression on macOS fixed by disabling lookahead when encountering invalid UTF-8 byte sequences.See commit ce025ae (20 Oct 2024) by René Scharfe (
rscharfe
).(Merged by Taylor Blau --
ttaylorr
-- in commit 43ac239, 01 Nov 2024)