C++ match_results prefix()、suffix()实例讲解(http://www.manongjc.com/detail/30-shytckdouzqxspf.html)
本文章向大家介绍C++ match_results prefix()、suffix()实例讲解,主要分析其语法、参数、返回值和注意事项,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。
- match_results::prefix()是C++中的内置函数,用于获取输入目标字符串中匹配字符串之前的字符串。
用法:
smatch_name.prefix()
Note:smatch_name is an object of match_results class.
参数:此函数不接受任何参数。
返回值:此函数返回目标字符串中匹配序列之前的序列。
注意:第一个元素始终包含整个正则表达式匹配项,而其他元素则包含特定的捕获组。
以下示例程序旨在说明上述函数:
// CPP program to illustrate
// match_results prefix() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s("Geeksforgeeks is a computer science portal");
regex re("computer");
smatch match;
regex_search(s, match, re);
cout << "Prefix is:[";
if (!match.empty()) {
cout << match.prefix() << "]" << endl;
}
return 0;
}
输出:
Prefix is:[Geeksforgeeks is a ]
- match_results::suffix()是C++中的内置函数,用于获取输入目标字符串中匹配字符串之后的字符串。
用法:
smatch_name.suffix()
Note:smatch_name is an object of match_results class.
参数:此函数不接受任何参数。
返回值:该函数返回目标字符串中匹配序列之后的序列。
注意:第一个元素始终包含整个正则表达式匹配项,而其他元素则包含特定的捕获组。
以下示例程序旨在说明上述函数:
// CPP program to illustrate
// match_results suffix() function
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s("Geeksforgeeks is a computer science portal");
regex re("computer");
smatch match;
regex_search(s, match, re);
cout << "Suffix is:[";
if (!match.empty()) {
cout << match.suffix() << "]" << endl;
}
return 0;
}
输出:
Suffix is:[ science portal]