Regular Expression iterator is not working in Cpp
I am working with C++ on Visual Studio 2010 (I don't think its v11
standard but I haven't checked).
I am trying to extract out the IP address of a tracert with the following
code:
#include <iomanip>
#include <iostream>
#include <string>
#include <regex>
using namespace std;
typedef regex_iterator<string::iterator> regexp;
#define MAX_BUFFER 255
int main() {
string out;
char buffer[MAX_BUFFER];
smatch m;
regex e(" 1.+\\[(.+)\\]");
FILE *stream = _popen("tracert SOMEHOSTNAME", "r");
while ( fgets(buffer, MAX_BUFFER, stream) != NULL ) {
out = buffer;
regexp rit (out.begin(), out.end(), e);
regexp rend;
while (rit != rend) {
cout << rit->str() << endl;
++rit;
}
}
_pclose(stream);
cout << "Done.";
cin >> buffer;
}
however, the regexp is not extracting out the group itself. Instead it is
just spitting back the full line!
I thought I was following examples very carefully but it seems I am not
using regex_iterator correctly.
1 - How best can I extract the IP from this string
(Side question - is there a C++ function that will go into the network and
get the IP from a hostname just like tracert our pint? Another that will
get the mac address just like arp -a)
No comments:
Post a Comment