git使用什么样的正则表达式

时间:2021-05-05 16:55:47

I'm trying to use the git diff --word-diff-regex= command and it seems to reject any types of lookaheads and lookbehinds. I'm having trouble pinning down what flavor of regex git uses. For example

我正在尝试使用git diff --word-diff-regex =命令,它似乎拒绝任何类型的前瞻和外观。我无法确定正则表达式git使用的风格。例如

git diff --word-diff-regex='([.\w]+)(?!>)'

Comes back as an invalid regular expression.

回来作为无效的正则表达式。

I am trying to get all the words that are not HTML tags. So the resulting matches of the regex should be 'Hello' 'World' 'Foo' 'Bar' for the below string

我试图得到所有不是HTML标签的单词。因此,正则表达式的结果匹配应为以下字符串的“Hello”'World''Foo''Bar'

<p> Hello World </p><p> Foo Bar </p>

1 个解决方案

#1


3  

The Git source uses regcomp and regexec, which are defined by POSIX 1003.2. The code to compile a diff regexp is:

Git源使用regcomp和regexec,它们由POSIX 1003.2定义。编译diff regexp的代码是:

            if (regcomp(ecbdata->diff_words->word_regex,
                        o->word_regex,
                        REG_EXTENDED | REG_NEWLINE))

which in POSIX means that these are "extended" regular expressions as defined here.

在POSIX中意味着这些是这里定义的“扩展”正则表达式。

(Not every C library actually implements the same POSIX REG_EXTENDED. Git includes its own implementation, which can be built in place of the system's.)

(并非每个C库实际上都实现了相同的POSIX REG_EXTENDED.Git包含它自己的实现,可以构建它来代替系统。)

Edit (per updated question): POSIX EREs have neither lookahead nor lookbehind, nor do they have \w (but [_[:alnum:]] is probably close enough for most purposes).

编辑(根据更新的问题):POSIX ERE既没有前瞻也没有后瞻,也没有\ w(但[_ [:alnum:]]可能已经足够接近大多数用途)。

#1


3  

The Git source uses regcomp and regexec, which are defined by POSIX 1003.2. The code to compile a diff regexp is:

Git源使用regcomp和regexec,它们由POSIX 1003.2定义。编译diff regexp的代码是:

            if (regcomp(ecbdata->diff_words->word_regex,
                        o->word_regex,
                        REG_EXTENDED | REG_NEWLINE))

which in POSIX means that these are "extended" regular expressions as defined here.

在POSIX中意味着这些是这里定义的“扩展”正则表达式。

(Not every C library actually implements the same POSIX REG_EXTENDED. Git includes its own implementation, which can be built in place of the system's.)

(并非每个C库实际上都实现了相同的POSIX REG_EXTENDED.Git包含它自己的实现,可以构建它来代替系统。)

Edit (per updated question): POSIX EREs have neither lookahead nor lookbehind, nor do they have \w (but [_[:alnum:]] is probably close enough for most purposes).

编辑(根据更新的问题):POSIX ERE既没有前瞻也没有后瞻,也没有\ w(但[_ [:alnum:]]可能已经足够接近大多数用途)。