在C ++中match_results运算符=
在本文中,我们将讨论C++STL中match_results运算符'='的工作,语法和示例。
什么是C++STL中的match_results?
std::match_results是一个类似于容器的特殊类,用于保存匹配的字符序列的集合。在此容器类中,正则表达式匹配操作可找到目标序列的匹配项。
什么是match_results运算符'='
Match_results运算符=是一个相等运算符,用于将值分配给match_results。运算符=用于将元素从一个match_results对象复制或移动到另一个对象。
语法
match_results1 = (match_results2);
参数
另一个match_results对象,我们必须将其数据复制到match_results对象。
返回值
这什么也不会返回。
示例
Input: string str = "Tutorials Point";
regex R("(Tutorials)(.*)");
smatch Mat_1, Mat_2;
regex_match(str, Mat_1, R);
Mat_2 = Mat_1;
Output: MAT 2 =
Tutorials Point
Tutorials
Point示例
#include <bits/stdc++.h>
using namespace std;
int main() {
string str = "Tutorials Point";
regex R("(Tutorials)(.*)");
smatch Mat_1, Mat_2;
regex_match(str, Mat_1, R);
Mat_2 = Mat_1;
cout<<"String matches: " << endl;
for (smatch::iterator i = Mat_2.begin(); i!= Mat_2.end(); i++) {
cout << *i << endl;
}
}输出结果
如果我们运行上面的代码,它将生成以下输出-
String matches: Tutorials Point Tutorials Point
示例
#include <bits/stdc++.h>
using namespace std;
int main() {
string str = "Tutorials Point";
regex R_1("(Tutorials)(.*)");
regex R_2("(Po)(int)(.*)");
smatch Mat_1, Mat_2;
regex_match(str, Mat_1, R_1);
regex_match(str, Mat_2, R_2);
smatch Mat;
if (Mat_1.size() > Mat_2.size()) {
Mat = Mat_1;
} else {
Mat = Mat_2;
}
cout<<"string matches " << endl;
for (smatch::iterator i = Mat.begin(); i!= Mat.end(); i++) {
cout << *i << endl;
}
}输出结果
如果我们运行上面的代码,它将生成以下输出-
String matches: Tutorials Point Tutorials Point