Regex

#include <iostream>
#include <cassert>
#include <string>
#include "boost/regex.hpp"

//完全匹配
void boostregex_match(void);

//部分数据可以匹配
void boostregex_search(void);

//替换
void boostregex_replace(void);

//关于重复和贪婪
void Test1(void);

//regex_iterator
void Test2(void);

//regex_token_iterator 
void Test3(void);

int main() 
{ 
	//boostregex_search();
	//boostregex_replace();
	//Test1();
	//Test2();
	Test3();
	return 0;
}

void boostregex_match(void)
{
	// 3 digits, a word, any character, 2 digits or "N/A",   // a space, then the first word again 
	//用于表示"3个数字, 一个单词, 任意字符, 2个数字或字符串"N/A," 一个空格, 然后重复第一个单词.":
	boost::regex reg("\\d{3}([a-zA-Z]+).(\\d{2}|N/A)\\s\\1");
	std::string correct = "123Hello N/A Hello";
	std::string incorrect = "123Hello 12 hello";
	assert(boost::regex_match(correct, reg) == true);
	assert(boost::regex_match(incorrect, reg) == false);
	return;
}

//部分数据可以匹配
void boostregex_search(void)
{
	// "new" and "delete" 出现的次数是否一样? 
	boost::regex reg("(new)|(delete)"); 
	boost::smatch m;  
	std::string s=    "Calls to new must be followed by delete. \     Calling simply new results in a leak!";  
	int new_counter=0; 
	int delete_counter=0; 
	std::string::const_iterator it=s.begin();  
	std::string::const_iterator end=s.end();  
	while (boost::regex_search(it,end,m,reg)) 
	{    // 是 new 还是 delete?  
		m[1].matched ? ++new_counter : ++delete_counter; 
		it=m[0].second; 
	} 
	if (new_counter!=delete_counter) 
		std::cout << "Leak detected!\n";  
	else    
		std::cout << "Seems ok...\n";
	return;
	/*
	Leak detected!
请按任意键继续. . .
	*/
}

//替换
void boostregex_replace(void)
{
	boost::regex reg("(Colo)(u)(r)", 
	boost::regex::icase | boost::regex::perl);  
	std::string s = "Colour, colours, color, colourize";  
	s = boost::regex_replace(s, reg, "$1$3");  
	std::cout << s;
	return;
	/*
	Color, colors, color, colorize请按任意键继续. . .
	*/
}

//关于重复和贪婪
void Test1(void)
{
	boost::regex reg("(.*)(\\d{2})");  
	boost::cmatch m;  
	const char* text = "Note that I'm 31 years old, not 32.";  
	if (boost::regex_search(text, m, reg)) {
		if (m[1].matched)      
			std::cout << "(.*) matched: " << m[1].str() << '\n';   
		if (m[2].matched)      
			std::cout << "Found the age: " << m[2] << '\n'; }
	return;
	/*
	(.*) matched: Note that I'm 31 years old, not
Found the age: 32
请按任意键继续. . .
	*/
}


class regex_callback 
{ 
	int sum_; 
public: 
	regex_callback() : sum_(0) {}  
	template <typename T> void operator()(const T& what) 
	{
		sum_ += atoi(what[1].str().c_str()); 
	}  
	int sum() const { return sum_; }

};

//regex_iterator
void Test2(void)
{
	boost::regex reg("(\\d+),?");  
	std::string s = "1,1,2,3,5,8,13,21";  
	boost::sregex_iterator it(s.begin(), s.end(), reg);  
	boost::sregex_iterator end;  regex_callback c;  
	int sum = for_each(it, end, c).sum();
	return;
}

//regex_token_iterator 
void Test3(void)
{
	boost::regex reg("/");  
	std::string s = "Split/Values/Separated/By/Slashes,"; 
	std::vector<std::string> vec;  
	boost::sregex_token_iterator it(s.begin(), s.end(), reg, -1);  
	boost::sregex_token_iterator end;  
	while (it != end)    
		vec.push_back(*it++); 

	assert(vec.size() == std::count(s.begin(), s.end(), '/') + 1);  
	assert(vec[0] == "Split");
	return;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值