1

我试过这个

git log -i --all --grep='/(?=.*fix)(?=.*a)(?=.*bug)/'

但没有用。

4

1 回答 1

3

有几个问题:

  • 使用grep时,正则表达式模式不会在正则表达式分隔符内传递,而是作为常规字符串传递
  • 您使用的符合 PCRE 的模式可能无法正常工作,因为默认的git grep正则表达式引擎是 POSIX BRE 风格
  • 您使用的模式匹配fixabug以任何顺序在同一行上,要求它们都存在。要以任何顺序匹配指定的字符串,您需要交替模式,例如a|b|c. 但是,在 POSIX BRE 中,不支持交替运算符,尽管 GNU 工具中可用的 POSIX 扩展允许使用\|交替运算符版本。

因此,如果您打算以任何顺序匹配具有这 3 个单词的条目,则需要删除正则表达式分隔符并启用 PCRE 正则表达式引擎:

git log -i -P --all --grep='^(?=.*fix)(?=.*a)(?=.*bug)'

请注意-P启用 PCRE 正则表达式引擎的选项。另外,请注意文档中的内容:

-P
--perl-regexp
Consider the limiting patterns to be Perl-compatible regular expressions.

Support for these types of regular expressions is an optional compile-time dependency. If Git wasn’t compiled with support for them providing this option will cause it to die.

如果您想将条目与任何单词匹配,您可以使用

git log -i -E --all --grep='fix|a|bug'

使用-E选项,POSIX ERE 语法被强制执行,并且|是这种正则表达式风格的交替模式。

要将它们作为整个单词进行匹配,请使用\b\</\>单词边界:

git log -i -E --all --grep='\<(fix|a|bug)\>'
git log -i -E --all --grep='\b(fix|a|bug)\b'

Windows 用户注意事项

在 Windows Git CMD 或 Windows 控制台中,'必须替换为"

git log -i -P --all --grep="^(?=.*fix)(?=.*a)(?=.*bug)"
git log -i -E --all --grep="\b(fix|a|bug)\b"
于 2022-02-21T08:50:15.840 回答