gpt4 book ai didi

c++ - C++11 中默认的正则表达式引擎是什么?

转载 作者:太空狗 更新时间:2023-10-29 22:56:38 24 4
gpt4 key购买 nike

从我的一位同事那里看到了正则表达式 <.*?>对于剥离 HTML 标签,零个或多个字符 ( ? ) 出现零个或一个 ( .* ) 对我来说没有意义。此外,自 .* 起似乎无法正常工作。是贪婪的,所以也会去除 标签之间的数据。

但是在在线正则表达式中对此进行测试时,我的同事关于它使它成为非贪婪的断言似乎是正确的,所以我决定去检查为什么。

无论如何,这只是背景。查看 C++11 标准,我终于发现根据 28.5.2 允许有多个可能的正则表达式引擎。和 table 128 ,并且:

A valid value of type syntax_option_type shall have exactly one of the elements ECMAScript, basic, extended, awk, grep, egrep, set.

但是,同一部分显示如果未指定则使用默认值 match_default , 被定义为零,这显然没有设置了其中一个位。

在标准中我找不到指定默认引擎的地方,所以我想知道这是实现定义还是我只是错过了它。

最佳答案

好的,根据评论,我更深入地研究了标准以及类层次结构是什么。

我相信这包含在 C++11 28.8.2 中的 basic_regex 构造函数中,由更高级别使用。所有这些构造函数,例如:

basic_regex(const charT* p, flag_type f = regex_constants::ECMAScript);

使用 ECMAScript 作为默认引擎。


而且,值得一提的是,ECMAScript 语法明确调用了 .*? 行为,这让我首先开始了这个探索。 ? 在紧跟量词 (*, >+?{exact}{min,}{min,max}) 因为它使匹配成为非贪婪的:

By default, all these quantifiers are greedy (i.e., they take as many characters that meet the condition as possible). This behavior can be overridden to ungreedy (i.e., take as few characters that meet the condition as possible) by adding a question mark, ?, after the quantifier.

For example, matching "(a+).*" against "aardvark" succeeds and yields aa as the first submatch, while matching "(a+?).*" against it also succeeds, but yields a as the first submatch.

关于c++ - C++11 中默认的正则表达式引擎是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47320905/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com