简体   繁体   English

正则表达式迭代器在Cpp中不起作用

[英]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). 我在Visual Studio 2010上使用C ++(我不认为它的v11标准,但我没有检查)。

I am trying to extract out the IP address of a tracert with the following code: 我试图用以下代码提取tracert的IP地址:

#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. 我以为我非常仔细地遵循了例子,但似乎我没有正确使用regex_iterator。

1 - How best can I extract the IP from this string 1 - 如何最好地从此字符串中提取IP

(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) (附带问题 - 是否有一个C ++函数将进入网络并从主机名获取IP就像跟踪我们的品脱?另一个将获得mac地址就像arp -a一样)

I don't have MSVC, and GNU has broken std::regex support, so I've played with `boost::regex' here: 我没有MSVC,GNU已经破坏了std :: regex支持,所以我在这里玩过'boost :: regex':

regex e("^\\s*1\\s.*?\\[(.*?)\\]");

Note this: 请注意:

  • doesn't assume spaces are spaces rather than tab characters 不假设空格是空格而不是制表符
  • doesn't assume exact spacing at the start of the line 不假设线的起始处有确切的间距
  • does mandate at least 1 space after the 1 character (so it won't match the line starting with 13 , eg) 强制至少1个空间1字符(因此它不会匹配线开始13 ,例如)
  • if uses non-greedy matching where possible 如果在可能的情况下使用非贪婪匹配
#include <iostream>
#include <string>
#include <boost/regex.hpp>

using namespace std;
using boost::regex;
using boost::regex_iterator;
#define MAX_BUFFER 255

int main()
{
    char buffer[MAX_BUFFER];
    regex e("^\\s*1\\s.*?\\[(.*?)\\]");

    FILE *stream = popen("cat input.txt", "r");
    while(fgets(buffer, MAX_BUFFER, stream) != NULL)
    {
        typedef regex_iterator<string::iterator> regit;

        string out = buffer;
        regit rit(out.begin(), out.end(), e);
        regit rend;
        while(rit != rend)
        {
            cout << (*rit)[1].str() << endl;
            ++rit;
        }
    }
    pclose(stream);
    cout << "Done.\n";
}

This appears to work for input.txt : 这似乎适用于input.txt

Tracing route to 11.1.0.1 over a maximum of 30 hops

1     2 ms     3 ms     2 ms  [157.54.48.1]
2    75 ms    83 ms    88 ms  [11.1.0.67]
3    73 ms    79 ms    93 ms  [11.1.0.1]

Trace complete.

printing: 打印:

157.54.48.1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM