C ++中的match_results prefix()和suffix()

在本文中,我们将讨论C ++ STL中match_results::prefix()和match_results::suffix()函数的工作原理,语法和示例。

什么是C ++ STL中的match_results?

std::match_results是一个类似于容器的特殊类,用于保存匹配的字符序列的集合。在此容器类中,正则表达式匹配操作可找到目标序列的匹配项。

什么是match_results::prefix()

match_results::prefix()函数是C ++ STL中的内置函数,在<regex>头文件中定义。prefix()用于获取与该函数关联的对象的先前match_results。此函数返回match_results对象的后续序列的引用。

语法

match_results.prefix();

参数

此函数不接受任何参数。

返回值

此函数返回匹配序列之前的字符串或序列的常量引用。

示例

Input: string str = "nhooo.coms";
   regex R("Points");
   smatch Mat;
   regex_search(str, Mat, R);
   Mat.prefix();
Output: Tutorials

字首()

示例

#include <bits/stdc++.h>
using namespace std;
int main() {
   string str = "nhooo.coms";
   regex R("Points");
   smatch Mat;
   regex_search(str, Mat, R);
   cout<<"String prefix is : ";
   if (!Mat.empty()) {
      cout << Mat.prefix();
   }
   return 0;
}

输出结果

如果我们运行上面的代码,它将生成以下输出-

String prefix is : Tutorials

什么是match_results::suffix()

match_results::suffix()函数是C ++ STL中的内置函数,在<regex>头文件中定义。suffix()用于获取与该函数关联的对象的后续match_results。此函数返回match_results对象的后续序列的引用。

语法

match_results.suffix();

参数

此函数不接受任何参数。

返回值

此函数返回匹配序列之后的字符串或序列的常量引用。

示例

Input: std::string str("nhooo.coms is the best");
   std::smatch Mat;
   std::regex re("Points");
   std::regex_match ( str, Mat, re );
   Mat.suffix();
Output: is the best

后缀()

示例

#include <bits/stdc++.h>
using namespace std;
int main() {
   string str = "nhooo.coms is the best";
   regex R("Points");
   smatch Mat;
   regex_search(str, Mat, R);
   cout<<"String prefix is : ";
   if (!Mat.empty()) {
      cout << Mat.suffix();
   }
   return 0;
}

输出结果

如果我们运行上面的代码,它将生成以下输出-

String prefix is : is the best