为什么使用Perl正则表达式的负回溯在git-log中会导致致命错误?

huangapple go评论63阅读模式
英文:

Why is git-log with perl regex negative lookbehind causing a fatal error?

问题

I cannot get git-log's regexp grep functionality to search using perl based regexp (pcre) or I can but it is not working with negative lookbehind.

git log -p -P -G'(?<!-)title'

results in output

fatal: invalid regex: Invalid preceding regular expression

However, pcre support and negative lookbehind seems to be working, if git-grep is any indicator.
E.g. git grep -P '(?<!-)title' returns expected results.

git version 2.21.0 (Apple Git-122.2)

英文:

Either I cannot get git-log's regexp grep functionality to search using perl based regexp (pcre) or I can but it is not working with negative lookbehind.

git log -p -P -G&#39;(?&lt;!-)title&#39;

results in output

fatal: invalid regex: Invalid preceding regular expression

However, pcre support and negative lookbehind seems to be working, if git-grep is any indicator.
E.g. git grep -P &#39;(?&lt;!-)title&#39; returns expected results.

git version 2.21.0 (Apple Git-122.2)

答案1

得分: 2

"-P" 不影响 "-G"。您可以通过将 "-P" 替换为 "-F"(或 --fixed-string)来验证这一点:即使在固定字符串中没有任何查找,您仍将收到相同的错误。

"-P" 的文档中提到

将限制模式视为 Perl 兼容的正则表达式。

注意这里的 "模式" 一词。确实,--grep 选项也提到了它:

--grep=

另一方面,"-G" 没有提到:

-G

英文:

The -P doesn't influence -G. You can verify it by replacing -P with -F (or --fixed-string): you'll still get the same error, even if there's no look-around at all in fixed strings.

The documentation of -P says

> Consider the limiting patterns to be Perl-compatible regular expressions.

Note the word "patterns". Indeed, the --grep options mentions it:

> --grep=&lt;pattern>

On the other hand, -G doesn't:

> -G&lt;regex>

huangapple
  • 本文由 发表于 2020年1月6日 15:35:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/59608275.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定