C++中不匹配组的位置是什么?
What is the position of an unmatched group in C++?
设 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)
returns(*this)[n].first
.
鉴于[如果] matched 为假,[then] 成员 first 和 second 指向被搜索序列的末尾 ...
这意味着 m.position(n)
应该指向“搜索到的序列的末尾”。
设 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)
returns(*this)[n].first
.
鉴于[如果] matched 为假,[then] 成员 first 和 second 指向被搜索序列的末尾 ...
这意味着 m.position(n)
应该指向“搜索到的序列的末尾”。