boost
1、匹配 InterGateway.Device.{i}.user 并替换为 InterGateway.Device.user
#include <iostream> #include <string> #include <boost/regex.hpp> boost::regex reg("^(InterGateway\\.Device\\.)\\d\\.(user|name)$"); int main(int argc, char **argv) { std::string str("InterGateway.Device.1.user"); if(argc == 2) { str = argv[1]; } std::cout << "test string:" << str << std::endl; boost::smatch what; if(boost::regex_match(str, what, reg)) { std::cout << "match" << std::endl; std::string s = boost::regex_replace(str, reg, "$1$2"); std::cout << "result:" << s << std::endl; } else { std::cout << "not match" << std::endl; } return 0; }
编译 g++ -o boost_regex_test boost_regex_test.cpp -lboost_regex
需要安装 boost库
下载:https://sourceforge.net/projects/boost/files/
安装:
tar -jxvf boost_1_53_0.tar.bz2
cd boost_1_53_0
./bootstrap.sh --with-libraries=regex
./b2
./b2 install --prefix=/usr