c++ - 在 C++ 中,unmatched group 的位置是什么?

标签 c++ regex regex-group

m类型为 std::smatch .假设有一个不匹配的组 i .什么是m.position(i) ?就此而言,什么是m[i] ?
例如,考虑

std::regex re {"^(a+)|(b+)"};
string target="aa";
std::smatch m;
std::regex_search(target,m,re);
cout<<"m[2] is: "<<m[2]<<" at position: "<<m.position(2);
我无法从引用 https://en.cppreference.com/w/cpp/regex/match_results/position 中弄清楚什么可以保证在这里发生以及为什么。

最佳答案

根据 C++17 标准:

28.10 Class template match_results [ re.results ]

4 The sub_match object stored at index 0 represents sub-expression 0, i.e., the whole match. In this case the sub_match member matched is always true. The sub_match object stored at index n denotes what matched the marked sub-expression n within the matched expression. If the sub-expression n participated in a regular expression match then the sub_match member matched evaluates to true, and members first and second denote the range of characters [first,second) which formed that match. Otherwise matched is false, and members first and second point to the end of the sequence that was searched.

[ Note: The sub_match objects representing different sub-expressions that did not participate in a regular expression match need not be distinct. — end note ]


现在 m.position(n)返回 (*this)[n].first .
鉴于“[If] 匹配为假,[then] 成员第一个和第二个指向被搜索序列的末尾”......
这意味着 m.position(n)应该指向“被搜索序列的末尾”。

关于c++ - 在 C++ 中,unmatched group 的位置是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66017185/

相关文章:

c++ - 我哪里错了?查找名称 Node 会找到结构 Node 的注入(inject)类名

c++ - 使用位的标志

java - 像在 C# 中一样在 Java 中捕获正则表达式

python - 正则表达式 Python/组量词

c++ - 构建 VTK 'INSTALL' 项目时出现 CMake 错误

php - 使用 preg_match 来匹配此特定模式

regex - 当正则表达式为变量时,analyzer-string不匹配

python - 如何在 Python 中跳过匹配模式的行?

javascript - 正则表达式不包括捕获组后的匹配

c++ - 使用 soundtouch 库在 iOS 中检测 BPM