Boost regex 在我的代码中没有按预期工作

2021-12-24 00:00:00 regex c++ boost

我今天刚开始使用 Boost::regex,我也是正则表达式的新手.我一直在使用The Regulator"和 Expresso 来测试我的正则表达式,并且似乎对我在那里看到的内容感到满意,但是将该正则表达式转移到 boost 似乎并没有做我想要它做的事情.任何帮助我解决方案的指针将是最受欢迎的.作为一个附带问题,是否有任何工具可以帮助我针对 boost.regex 测试我的正则表达式?

I just started using Boost::regex today and am quite a novice in Regular Expressions too. I have been using "The Regulator" and Expresso to test my regex and seem satisfied with what I see there, but transferring that regex to boost, does not seem to do what I want it to do. Any pointers to help me a solution would be most welcome. As a side question are there any tools that would help me test my regex against boost.regex?

using namespace boost;
using namespace std;

vector<string> tokenizer::to_vector_int(const string s)
{
    regex re("\d*");
    vector<string> vs;
    cmatch matches;
    if( regex_match(s.c_str(), matches, re) ) {
        MessageBox(NULL, L"Hmmm", L"", MB_OK); // it never gets here
        for( unsigned int i = 1 ; i < matches.size() ; ++i ) {
            string match(matches[i].first, matches[i].second);
            vs.push_back(match);
        }
    }
    return vs;
}

void _uttokenizer::test_to_vector_int() 
{
    vector<string> __vi = tokenizer::to_vector_int("0<br/>1");
    for( int i = 0 ; i < __vi.size() ; ++i ) INFO(__vi[i]);
    CPPUNIT_ASSERT_EQUAL(2, (int)__vi.size());//always fails
}

更新(感谢 Dav 帮助我澄清我的问题):我希望得到一个包含 2 个字符串的向量 =>0"和1".相反,我从未获得成功的 regex_match()(regex_match() 总是返回 false),因此向量始终为空.

Update (Thanks to Dav for helping me clarify my question): I was hoping to get a vector with 2 strings in them => "0" and "1". I instead never get a successful regex_match() (regex_match() always returns false) so the vector is always empty.

感谢1800 信息"的建议.to_vector_int() 方法现在看起来像这样,但它进入了一个永无止境的循环(我拿了你给的代码并修改了它以使其可编译)并找到0",",","等等.它永远找不到1".

Thanks '1800 INFORMATION' for your suggestions. The to_vector_int() method now looks like this, but it goes into a never ending loop (I took the code you gave and modified it to make it compilable) and find "0","","","" and so on. It never find the "1".

vector<string> tokenizer::to_vector_int(const string s)
{
    regex re("(\d*)");
    vector<string> vs;

    cmatch matches;

    char * loc = const_cast<char *>(s.c_str());
    while( regex_search(loc, matches, re) ) {
        vs.push_back(string(matches[0].first, matches[0].second));
        loc = const_cast<char *>(matches.suffix().str().c_str());
    }

    return vs;
}

老实说,我认为我还没有理解搜索模式和获取匹配项的基础知识.是否有任何带有示例的教程可以解释这一点?

In all honesty I don't think I have still understood the basics of searching for a pattern and getting the matches. Are there any tutorials with examples that explains this?

推荐答案

基本问题是你使用了 regex_match 而你应该使用 regex_search :

The basic problem is that you are using regex_match when you should be using regex_search:

算法 regex_search 和regex_match 使用 match_results报告匹配的内容;区别这些算法之间是regex_match 只会找到匹配项消耗所有输入文本,其中 regex_search 将搜索文本中任意位置的匹配匹配.

The algorithms regex_search and regex_match make use of match_results to report what matched; the difference between these algorithms is that regex_match will only find matches that consume all of the input text, where as regex_search will search for a match anywhere within the text being matched.

来自 boost 文档.将其更改为使用 regex_search 即可.

From the boost documentation. Change it to use regex_search and it will work.

此外,您似乎没有捕获匹配项.尝试将正则表达式更改为:

Also, it looks like you are not capturing the matches. Try changing the regex to this:

regex re("(\d*)");

或者,也许您需要重复调??用 regex_search:

Or, maybe you need to be calling regex_search repeatedly:

char *where = s.c_str();
while (regex_search(s.c_str(), matches, re))
{
  where = m.suffix().first;
}

这是因为您的正则表达式中只有一个捕获.

This is since you only have one capture in your regex.

或者,如果您知道数据的基本结构,请更改您的正则表达式:

Alternatively, change your regex, if you know the basic structure of the data:

regex re("(\d+).*?(\d+)");

这将匹配搜索字符串中的两个数字.

This would match two numbers within the search string.

请注意,正则表达式 d* 将匹配零个或多个数字 - 这包括空字符串 "",因为这恰好是零个数字.我会将表达式更改为 d+,它将匹配 1 个或多个.

Note that the regular expression d* will match zero or more digits - this includes the empty string "" since this is exactly zero digits. I would change the expression to d+ which will match 1 or more.

相关文章