让 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/