C++ match_results prefix()、suffix()实例讲解

本文详细介绍了C++中的match_results类的prefix()和suffix()函数,包括它们的语法、参数、返回值以及使用技巧,并通过示例展示了如何在实际编程中应用。
摘要由CSDN通过智能技术生成

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]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值