pcre2_substitute() 函数支持多少捕获组?

How many captured groups are supported by pcre2_substitute() function?

我在我的 C++ 项目中使用 pcre2_substitute() 函数来执行正则表达式替换:

int ret=pcre2_substitute(
  re,                    /*Points to the compiled pattern*/
  subject,               /*Points to the subject string*/
  subject_length,        /*Length of the subject string*/
  0,                     /*Offset in the subject at which to start matching*/
  rplopts,               /*Option bits*/
  0,                     /*Points to a match data block, or is NULL*/
  0,                     /*Points to a match context, or is NULL*/
  replace,               /*Points to the replacement string*/
  replace_length,        /*Length of the replacement string*/
  output,                /*Points to the output buffer*/
  &outlengthptr          /*Points to the length of the output buffer*/
);

This is the man page of the function。它没有说有多少捕获组是可能的。我已经测试过 </code>、<code></code> 有效,但限制是多少?</p> <p>我检查了是否有像 C++ <code>std::regex 那样的数字限制,但没有。 [=17=]0000000000001 作为 </code> 工作,而在 <code>std::regex 中它意味着 [=20=],其余的将被视为字符串。

我用来测试的代码是this one。 您将需要 pcre2 库来 运行 此代码。

PCRE2 包

pcre2-10.20\README中写道,PCRE2 有一个计数器,可以限制模式中括号嵌套的深度。这限制了模式在编译时使用的系统堆栈量。默认是250,但是你可以通过设置来改变,比如--with-parens-nest-limit=500PCRE2有一个计数器可以设置来限制它在匹配时使用的资源量一种模式。如果在匹配过程中超过限制,则匹配失败。默认为一千万。您可以通过设置更改默认值,例如 --with-match-limit=500000.

所以,好像反向引用的数量

  • 未硬编码到 PCRE2
  • 如果匹配限制匹配限制递归参数,
  • 很可能取决于大小。

由于您是自己构建库,因此可以进一步增加。

PCRE 在线参考

来自 regular-expressions.info:

Most regex flavors support up to 99 capturing groups and double-digit backreferences. So </code> is a valid backreference if your regex has 99 capturing groups.</p> </blockquote> <p>和<a href="http://www.regular-expressions.info/refcapture.html" rel="nofollow noreferrer"><em>Regular Expression Reference: Capturing Groups and Backreferences</em> page</a>:</p> <blockquote> <p>Backreference <code> through </code> <br/> Backreference <code> through </code></p> </blockquote> <p>请注意,作者所说的“大多数”必须指的是主要的正则表达式引擎(PHP、JavaScript、Python、.NET),而不是 POSIX BRE、POSIX ERE、GNU BRE、GNU ERE 正则表达式风格,仅支持最多 <code>.

的反向引用

但是,在pcre.txt中,有一行

\ddd     character with octal code ddd, or backreference

所以,根据这个文档,可以有999个组。

捕获组的最大数量为65,535。这也是模式或替换中可以反向引用的最大组数。

但是,一般来说,一场比赛可能会达到另一个限制,然后才能允许那么多组:例如主题字符串的最大长度,或内部调用 match() 的次数(总计或递归),但可以增加匹配限制。有关匹配限制的详细信息,请参阅“The match context" in pcre2api.


来自pcre2limits man page

There is no limit to the number of parenthesized subpatterns, but there can be no more than 65,535 capturing subpatterns.

There is, however, a limit to the depth of nesting of parenthesized subpatterns of all kinds. This is imposed in order to limit the amount of system stack used at compile time. The limit can be specified when PCRE2 is built; the default is 250.

The maximum number of named subpatterns is 10,000.

作者:菲利普黑兹尔。最后更新:2014 年 11 月 25 日。- *截至 PCRE2 版本 10.20


PCRE and PCRE2

中的大小限制

PCRE 和 PCRE2 具有相同的限制:

  • 重复 量词 中的所有值限制为 65,535。

  • 无限数量的带括号的子模式
    (尽管它仅限于各种带括号的子模式的嵌套深度)。

  • 65,535 正在捕获子模式

  • 10,000 个 命名子模式

  • 嵌套括号的默认最大深度为250
    PCRE2_CONFIG_PARENSLIMIT 的值)。

  • 命名子模式的 names 的最大长度为 32 个代码单元。
    一个字符由 1+ code units 表示(取决于编码)。例如。在 UTF-8 中,“Ç”有 2 个代码单元:0xC3 0x87

  • 没有限制后向引用.

  • 对后续子模式的前向引用的数量限制在 200,000 左右。

  • 控制动词中使用的名称限制为 255(8 位)和 65,535(16 或 32 位)。

  • PCRE2_CONFIG_MATCHLIMIT 的默认值为 10,000,000 (10m)。

  • PCRE2_CONFIG_RECURSIONLIMIT 的默认值为 10,000,000 (10m)。
    此限制仅在设置小于 MATCH_LIMIT 时适用)。

  • 如果使用默认内部链接大小 2 编译,编译模式 的最大长度为 64K 代码单元(参见 pcre2build documentation详情)。

  • 主题字符串的最大长度是整数变量可以容纳的最大正数(可能是~1.8E+19)。但是,可用堆栈 space 可能会限制某些模式可以处理的主题字符串的大小。
    主题字符串的最大长度(以代码单元为单位)比 PCRE2_SIZE 变量可以容纳的最大数字少一。 PCRE2_SIZE是无符号整数类型,通常定义为size_t.