C++中的结果操作符=

这个 匹配结果::运算符= 用于将smatch对象中的所有匹配项替换为另一个smatch对象中的新匹配项。 语法:

null
smatch_name1 = (smatch_name2)Note: smatch_name is an object of match_results class.

参数: 右侧的smatch对象被复制到左侧的smatch对象。 返回值: 它不会返回任何内容。 注: 第一个元素总是包含整个正则表达式匹配项,而其他元素则包含特定的正则表达式匹配项 捕获群 . 下面的程序说明了上述功能。 项目1:

CPP

// CPP program to illustrate
// match_results operator= in C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s( "Geeksforgeeks" );
regex re( "(Geeks)(.*)" );
smatch match1, match2;
regex_match(s, match1, re);
// use of operator =--> assigns all
// the contents of match1 to match2
match2 = match1;
cout << "Matches are:" << endl;
for (smatch::iterator it = match2.begin();
it != match2.end(); it++) {
cout << *it << endl;
}
}


输出:

Matches are:GeeksforgeeksGeeksforgeeks

项目2:

CPP

// CPP program to illustrate
// match_results operator= in C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s( "Geeksforgeeks" );
regex re1( "(Geeks)(.*)" );
regex re2( "(Ge)(eks)(.*)" );
smatch match1, match2;
regex_match(s, match1, re1);
regex_match(s, match2, re2);
smatch max_match;
// use of operator =--> assigns all
// the contents of match1 to match2
if (match1.size() > match2.size()) {
max_match = match1;
}
else {
max_match = match2;
}
cout << "Smatch with maximum matches:" << endl;
for (smatch::iterator it = max_match.begin();
it != max_match.end(); it++) {
cout << *it << endl;
}
}


输出:

Smatch with maximum matches:GeeksforgeeksGeeksforgeeks

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享