vern@2006-10-16:/var% ls tags

解析ifconfig输出内容的正则表达式

使用boost::regex库,首先安装libboost-regex-dev

sudo apt-get install libboost-regex-dev

#include

include

include

include

using namespace boost; using namespace std;

int main() {     string str = ""         "        eth1      Link encap:Ethernet  HWaddr 00:E0:4C:9E:B5:EF\n"         "          inet addr:192.168.0.193  Bcast:192.168.0.255   Mask:255.255.255.0\n"         "          inet6 addr: fe80::2e0:4cff:fe9e:b5ef/64 Scope:Link\n"         "          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1\n"         "          RX packets:25608 errors:0 dropped:0 overruns:0 frame:0\n"         "          TX packets:23396 errors:0 dropped:0 overruns:0 carrier:0\n"         "          collisions:0 txqueuelen:1000\n"         "          RX bytes:27459152 (26.1 MiB)  TX bytes:3307863 ( 3.1 MiB)\n"         "          Interrupt:11 Base address:0xec00\n";

    string exp = ""         "\s+hwaddr\s+(.{17})"         "\s+inet addr:(\d+.\d+.\d+.\d+)"         ".mask:(\d+.\d+.\d+.\d+)"         ".(\w{2})\s+broadcast.mtu:(\d+).*metric:(\d+)"         ".*packets:(\d+)\s+errors:(\d+)\s+dropped:(\d+)\s+overruns:(\d+)\s+frame:(\d+)"         ".packets:(\d+)\s+errors:(\d+)\s+dropped:(\d+)\s+overruns:(\d+)\s+carrier:(\d+)"         ".*bytes:(\d+).*bytes:(\d+)";

    regex expression(exp, regbase::extended | regbase::icase);     deque result;     regex_split(std::back_inserter(result), str, expression);

    //copy(result.begin(),result.end(),ostream_iterator(cout,"\n"));     deque::iterator it = result.begin();     for (; it != result.end(); ++it) {         cout<<"("<<*it<<")"<<endl;     }

    return 0; }

g++ test.cpp -lboost_regex

./a.out

(00:E0:4C:9E:B5:EF) (192.168.0.193) (255.255.255.0) (UP) (1500) (1) (25608) (0) (0) (0) (0) (23396) (0) (0) (0) (0) (27459152) (3307863)