boost::regexでIPアドレスを取得

備忘録。

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

int tmpIP[4] = {0,0,0,0}, tmpPort = 0;
std::string str(buf);
boost::regex reg("(\\d+)\.(\\d+)\.(\\d+)\.(\\d+):(\\d+)");
boost::regex reg2("(\\d+)\.(\\d+)\.(\\d+)\.(\\d+)");
boost::smatch result;
if (boost::regex_search(str, result, reg)) {
	for(int i=0;i<4;i++) tmpIP[i] = atoi(result.str(i+1).c_str());
	tmpPort = atoi(result.str(5).c_str());
}
else if (boost::regex_search(str, result, reg2)) {
	for(int i=0;i<4;i++) tmpIP[i] = atoi(result.str(i+1).c_str());
}

atoiはlexical_castとか使った方が良い。