用boost分割字符串的代码示例

<pre name="code" class="cpp">#include "stdafx.h"

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


/*
测试环境
[1]VS2010SP1,boost 1.55
*/

void testTokenizer()
{
	std::wstring str = L"abc,def gul,中文,古 典,答辩";

	typedef boost::tokenizer<boost::char_separator<wchar_t>
		, std::wstring::const_iterator, std::wstring> Token;
	//分隔符可以有多个比如sep(" ,:"),这样空格、逗号肯分号,分别都
	//可以用来分隔字符串
	boost::char_separator<wchar_t> sep(L",");
	Token tok(str,sep); 

	for (Token::iterator it = tok.begin(); it != tok.end(); ++it) 
	{
		std::wcout << *it << std::endl;
	}
}

bool isInteger(std::wstring wstr)
{
	boost::basic_regex<wchar_t, boost::regex_traits<wchar_t> >expr(L"^[1-9]\\d*$");

	if (boost::regex_match(wstr.c_str(),expr))
	{
		return true;
	}
	return false;
}

int _tmain(int argc, _TCHAR* argv[])
{
	testTokenizer();

	assert( isInteger(std::wstring(L"101")) ==true );
	assert( isInteger(std::wstring(L"10.1"))==false );

	return 0;
}



你可能感兴趣的:(字符串分割,正则)