为什么在正则表达式中“?紧跟在括号之后”是一个语法错误?

Why was "? immediately after a parenthesis" a syntax error, in Regular Expressions?

Python 正则表达式 HOWTO 解释了 non-capturing and named groups 的语法是如何产生的:

For these new features the Perl developers couldn’t choose new single-keystroke metacharacters or new special sequences beginning with \ without making Perl’s regular expressions confusingly different from standard REs. If they chose & as a new metacharacter, for example, old expressions would be assuming that & was a regular character and wouldn’t have escaped it by writing \& or [&].

The solution chosen by the Perl developers was to use (?...) as the extension syntax. ? immediately after a parenthesis was a syntax error because the ? would have nothing to repeat, so this didn’t introduce any compatibility problems.

我不明白为什么括号里要有重复的东西? 我确实理解总体要点,即采用导致语法错误的内容来扩展正则表达式功能,将防止现有正则表达式被破坏。

regular-expressions.info 解释得很好。

The ... question mark is the quantifier that makes the previous token optional. This quantifier cannot appear after an opening parenthesis, because there is nothing to be made optional at the start of a group. Therefore, there is no ambiguity between the question mark as an operator to make a token optional and the question mark as part of the syntax for non-capturing groups...

没有什么是可选的,因为 ? 之前的标记是组开始元字符 (,而不是在字符串中可搜索的东西,例如常规字符。

我不同意您链接到的 HOWTO 中的措辞。可选——即零次或一次——不是“重复”。