1. <legend id='CIqA2'><style id='CIqA2'><dir id='CIqA2'><q id='CIqA2'></q></dir></style></legend>
  2. <tfoot id='CIqA2'></tfoot>
    <i id='CIqA2'><tr id='CIqA2'><dt id='CIqA2'><q id='CIqA2'><span id='CIqA2'><b id='CIqA2'><form id='CIqA2'><ins id='CIqA2'></ins><ul id='CIqA2'></ul><sub id='CIqA2'></sub></form><legend id='CIqA2'></legend><bdo id='CIqA2'><pre id='CIqA2'><center id='CIqA2'></center></pre></bdo></b><th id='CIqA2'></th></span></q></dt></tr></i><div id='CIqA2'><tfoot id='CIqA2'></tfoot><dl id='CIqA2'><fieldset id='CIqA2'></fieldset></dl></div>

  3. <small id='CIqA2'></small><noframes id='CIqA2'>

        <bdo id='CIqA2'></bdo><ul id='CIqA2'></ul>

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

      时间:2023-07-18
      • <small id='LCaUO'></small><noframes id='LCaUO'>

            <legend id='LCaUO'><style id='LCaUO'><dir id='LCaUO'><q id='LCaUO'></q></dir></style></legend>
              <bdo id='LCaUO'></bdo><ul id='LCaUO'></ul>
              <tfoot id='LCaUO'></tfoot>
              <i id='LCaUO'><tr id='LCaUO'><dt id='LCaUO'><q id='LCaUO'><span id='LCaUO'><b id='LCaUO'><form id='LCaUO'><ins id='LCaUO'></ins><ul id='LCaUO'></ul><sub id='LCaUO'></sub></form><legend id='LCaUO'></legend><bdo id='LCaUO'><pre id='LCaUO'><center id='LCaUO'></center></pre></bdo></b><th id='LCaUO'></th></span></q></dt></tr></i><div id='LCaUO'><tfoot id='LCaUO'></tfoot><dl id='LCaUO'><fieldset id='LCaUO'></fieldset></dl></div>
                  <tbody id='LCaUO'></tbody>

                本文介绍了Boost regex 在我的代码中没有按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我今天刚开始使用 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.

                这篇关于Boost regex 在我的代码中没有按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:在同一个头文件中包含 guard 和 #pragma once 下一篇:Boost.Spirit mini_xml2.cpp 示例无法被 C++11, Boost 1.55 编译

                相关文章

                <small id='0BmuY'></small><noframes id='0BmuY'>

              1. <legend id='0BmuY'><style id='0BmuY'><dir id='0BmuY'><q id='0BmuY'></q></dir></style></legend>

                1. <tfoot id='0BmuY'></tfoot>
                    <bdo id='0BmuY'></bdo><ul id='0BmuY'></ul>

                    <i id='0BmuY'><tr id='0BmuY'><dt id='0BmuY'><q id='0BmuY'><span id='0BmuY'><b id='0BmuY'><form id='0BmuY'><ins id='0BmuY'></ins><ul id='0BmuY'></ul><sub id='0BmuY'></sub></form><legend id='0BmuY'></legend><bdo id='0BmuY'><pre id='0BmuY'><center id='0BmuY'></center></pre></bdo></b><th id='0BmuY'></th></span></q></dt></tr></i><div id='0BmuY'><tfoot id='0BmuY'></tfoot><dl id='0BmuY'><fieldset id='0BmuY'></fieldset></dl></div>