在本文中,我们将讨论C ++ STL中match_results::prefix()和match_results::suffix()函数的工作原理,语法和示例。
std::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
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