c++使用boost正则表达式的简单用法

本文介绍了一个使用Boost库进行正则表达式搜索与替换的例子。通过具体的代码演示了如何利用Boost库中的regex_search和regex_replace函数来实现字符串的匹配与替换操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分别为regex_search 和 regex_replace

#include <iostream>
#include <boost/regex.hpp>

using namespace std;

void main()
{
// 	const char *szReg = "(.*?)(1)(.*)";
//     const char *szStr = "ARIX11";
//    	boost::smatch mat;
// 	try
// 	{ 
// 		boost::regex reg( szReg );
// 		bool r=boost::regex_search( szStr, mat, reg);
// 		if(r) 
// 		{
// 			cout << mat.size() << endl;
// 			for(size_t i = 0; i < mat.size(); ++i)
// 			{
// 				
// 				if (mat[i].matched)
// 				{
// 					std::cout << mat[i] << std::endl;
// 					std::string sTest = mat[i].second;
// 				}
// 			}
// 		}
// 	}
// 	catch (exception& e)
// 	{
// 		return;
// 	}

	string s1 = "(.*?)(1)(.*)";
    string s2 = "($1)2($3)";
    boost::regex reg( s1 );
    string s = boost::regex_replace( string("ARIX11"), reg, s2, boost::match_default|boost::format_all);
    cout << s << endl;
	char* a;
	cin >> a;

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值