match_results size()是C++中的内置函数,它返回match_result对象中的matchs和sub-matches数。
句法:
smatch_name.size()
Note: smatch_name is an object of match_results class.
参数:该函数接受任何参数。
返回值:返回match_result对象中的匹配数。
注意:第一个元素始终包含整个正则表达式匹配项,而其他则包含特定的捕获组
下面的程序说明了上述函数。
程序1:
// CPP program to illustrate
// match_results size() in C++
#include
using namespace std;
int main()
{
string s("Geeksforgeeks");
regex re("(Geeks)(.*)");
smatch match;
// Function call to find match
regex_match(s, match, re);
cout << "match size is " << match.size() << endl;
return 0;
}
输出:
match size is 3
程式2:
// CPP program to illustrate
// match_results size() in C++
#include
using namespace std;
int main()
{
string s("Geeksforgeeks");
regex re("(Geeks)(.*)");
smatch match;
// Function call to find matches
regex_match(s, match, re);
cout << "match size is " << match.size() << endl;
for (int i = 0; i < match.size(); i++) {
cout << "match " << i << " has a length of "
<< match.length(i) << endl;
}
return 0;
}
输出:
match size is 3
match 0 has a length of 13
match 1 has a length of 5
match 2 has a length of 8
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。