这个 匹配结果::长度() 是C++中的内置函数,用于返回MatCHiTrices对象中特定匹配的长度。 语法:
null
smatch_name.length(n)Note: smatch_name is an object of match_results class.
参数: 它接受一个指定匹配号的参数n。它低于match_results::size。匹配号0表示整个匹配表达式。后续匹配号标识子表达式(如果有)。传递的整数是无符号整数类型。 返回值: 它返回的是 第n位 匹配结果对象中的匹配。 注: 第一个元素总是包含整个正则表达式匹配项,而其他元素则包含特定的正则表达式匹配项 捕获群 . 下面的程序说明了上述功能。 项目1:
CPP
// CPP program to illustrate // match_results length() in C++ #include <bits/stdc++.h> using namespace std; int main() { string s( "Geeksforgeeks" ); regex re( "(Geeks)(.*)" ); smatch match; regex_match(s, match, re); for ( int i = 0; i < match.size(); i++) { cout << "match " << i << " has a length of " << match.length(i) << endl; } return 0; } |
输出:
match 0 has a length of 13match 1 has a length of 5match 2 has a length of 8
项目2:
CPP
// CPP program to illustrate // match_results length() in C++ #include <bits/stdc++.h> using namespace std; int main() { string s( "Geeksforgeeks" ); regex re( "(Ge)(eks)(.*)" ); smatch match; regex_match(s, match, re); int max_length = 0; string str; // Since the first match is the // whole string we do not consider it. for ( int i = 1; i < match.size(); i++) { if (match.length(i) > max_length) { str = match[i]; max_length = match.length(i); } } cout << "max-length sub match is " << str << " with a length of " << max_length; return 0; } |
输出:
max-length sub match is forgeeks with a length of 8
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END