Example #1
0
File: main.cpp Project: CCJY/coliru
int main()
{
    std::string s = "for a good time, call 867-5309";
    std::regex phone_regex("\\d{3}-\\d{4}");
    std::smatch phone_match;
 
    if (std::regex_search(s, phone_match, phone_regex)) {
        std::string fmt_s = phone_match.format(
            "$`"    // $` means characters before the match
            "[$&]"  // $& means the matched characters
            "$'");  // $' means characters following the match
        std::cout << fmt_s << '\n';
    }   
}
Example #2
0
int main()
{
	std::cout<<"test regex simple usage:\n";
	std::string email_pattern("(\\w)+(\\.\\w+)*@(\\w)+((\\.\\w+)+)");
	try
	{
		std::regex email_regex(email_pattern);
		std::smatch results;
		std::string test_email_str = "My email is [email protected]";
		if(std::regex_search(test_email_str, results, email_regex))
		{
			std::cout<<results.str()<<std::endl;
		}
	}
	catch (std::regex_error e)
	{
		std::cout<<e.what()<<'\t'<<e.code()<<std::endl;
	}
	std::cout<<"test regex simple usage done.\n"<<std::endl;

	std::cout<<"test regex icase:\n";
	try
	{
		std::regex cpp_regex("(\\w)+\\.(cpp|hpp)$", std::regex::icase);
		std::vector<std::string> test_filenames = {"regex.cpp", "iostream.h", "template.CPP", "class.hPP", "Ah, regex.cpp", "regex.cpp Ah"};
		for(auto fn : test_filenames)
		{
			if(std::regex_match(fn, cpp_regex))
			{
				std::cout<<"cpp file: "<<fn<<'\n';
			}
		}
	}
	catch (std::regex_error e)
	{
		std::cout<<e.what()<<'\t'<<e.code()<<std::endl;
	}
	std::cout<<"test regex icase done.\n"<<std::endl;
	
	std::cout<<"test regex iterator usage:\n";
	try
	{
		std::regex email_regex(email_pattern);
		std::smatch results;
		std::string test_email_str = "I have three emails, [email protected], [email protected] and [email protected].";
		for(std::sregex_iterator it(std::begin(test_email_str), std::end(test_email_str), email_regex), end_it; it != end_it; it++)
		{
			std::cout<<it->str()<<std::endl;
		}
	}
	catch (std::regex_error e)
	{
		std::cout<<e.what()<<'\t'<<e.code()<<std::endl;
	}
	std::cout<<"test regex iterator usage done.\n"<<std::endl;
	
	std::cout<<"test regex sub_match usage:\n";
	std::string phone_pattern = "(\\()?(\\d{3,4})(\\))?([- ])?(\\d{7,8})";
	try
	{
		std::regex phone_regex(phone_pattern);
		std::smatch results;
		std::vector<std::string> test_phones = {"010-82224567", "(010-83332345", "(020)62334567", "(021) 22346543", "0357-4223456", "0358-465788"};
		for(auto fn : test_phones)
		{
			if(std::regex_match(fn, results, phone_regex))
			{
				if(results[1].matched)
				{
					if(!results[3].matched) continue;
					if(results[4].matched && results[4].str() == "-") continue;
				}
				else
				{
					if(results[3].matched) continue;
					if(!(results[4].matched && results[4].str() == "-")) continue;
				}
				std::cout<<results.str()<<std::endl;
			}
		
		}
	}
	catch (std::regex_error e)
	{
		std::cout<<e.what()<<'\t'<<e.code()<<std::endl;
	}
	std::cout<<"test regex sub_match usage done.\n"<<std::endl;;
	
	std::cout<<"test regex replace usage:\n";
	try
	{
		std::string format = "$2-$5";
		std::regex phone_regex(phone_pattern);
		std::string ori_phone = "yubo: (020)85776452";
		std::cout<<"formated phone: "<<std::regex_replace(ori_phone, phone_regex, format) <<std::endl;
	}
	catch (std::regex_error e)
	{
		std::cout<<e.what()<<'\t'<<e.code()<<std::endl;
	}
	std::cout<<"test regex replace usage done.\n"<<std::endl;;
}