boost::regex_search拒绝接受我的论点

boost::regex_search refuses to take my arguments

本文关键字:我的 拒绝 regex search boost      更新时间:2023-10-16

我在这件事上很挣扎,我已经到了没有任何进展的地步,是时候寻求帮助了。我对boost库的熟悉程度只比肤浅的要好一点。我正试着对一根相当大的绳子进行逐行扫描。事实上,它是读取到std::string对象中的文件的全部内容(文件不会那么大,它是命令行程序的输出)。

这个程序的输出pnputil是重复的。我正在寻找某些模式,以努力找到我想要的"oemNNN.inf"文件。从本质上讲,我的算法是找到第一个"oemNNN.inf",搜索该文件的识别特征。如果它不是我想要的,继续下一个。

在代码中,它类似于:

std::string filesContents;
std::string::size_type index(filesContents.find_first_of("oem"));
std::string::iterator start(filesContents.begin() + index);
boost::match_results<std::string::const_iterator> matches;
while(!found) {
    if(boost::regex_search(start, filesContents.end(), matches, re))
    {
        // do important stuff with the matches
        found = true; // found is used outside of loop too
        break;
    }
    index = filesContents.find_first_of("oem", index + 1);
    if(std::string::npos == index) break;
    start = filesContents.being() + index;
}

我使用的这个例子来自1.47的boost库文档(我使用的版本)。请有人向我解释一下我的用法与本例的用法有何不同(除了我没有将内容存储到地图等中之外)。

据我所知,我使用的迭代器类型与示例中使用的相同。然而,当我编译代码时,微软的编译器告诉我:没有重载函数boost::regex_search的实例匹配参数列表。然而,intellisense用我正在使用的参数显示了这个函数,尽管迭代器的名称是BidiIterator。我不知道这有什么意义,但在给定的例子中,我假设无论BidiIterator是什么,它都需要一个std::string::迭代器来进行构建(也许这是一个糟糕的假设,但在给出这个例子时似乎是有意义的)。该示例确实显示了第五个参数match_flags,但该参数默认为值:boost::match_default。因此,这应该是不必要的。然而,只是为了好玩,我添加了第五个论点,但它仍然不起作用。我怎么会滥用这些论点?尤其是在考虑示例时。

下面是一个简单的程序,它演示了没有循环算法的问题。

#include <iostream>
#include <string>
#include <boost/regex.hpp>
int main() {
std::string haystack("This is a string which contains stuff I want to find");
boost::regex needle("stuff");
boost::match_results<std::string::const_iterator> what;
if(boost::regex_search(haystack.begin(), haystack.end(), what, needle, boost::match_default)) {
    std::cout << "Found some matches" << std::endl;
    std::cout << what[0].first << std::endl;
}
return 0;
}

如果您决定编译,我将针对boost库的1.47进行编译和链接。我正在处理的项目广泛使用这个版本,更新不是由我决定的。

谢谢你的帮助。这是最令人沮丧的。

Andy

通常迭代器的类型是不同的。

std::string haystack("This is a string which contains stuff I want to find");

从CCD_ 1和CCD_ 2返回的值将是CCD_。但你的匹配类型是

boost::match_results<std::string::const_iterator> what;

CCD_ 4和CCD_。所以的变体很少

  1. 将字符串声明为常量(即const std::string haystack;
  2. 将迭代器声明为const_iterator(即std::string::const_iterator begin = haystack.begin(), end = haystack.end();),并将它们传递给regex_search
  3. 使用boost::match_results<std::string::iterator> what;
  4. 如果您有C++11,则可以使用begin()0和haystack.cend()

的工作示例