Avoid having $ match end of buffer with boost regex partial_match

0

I am regex searching a file with cregex_iterator. I have boost::regex_constants::match_partial and boost::regex_constants::match_not_eob set in my match flags and I have $ at the end of my regular expression. The problem is that $ appears to be matching against the end of the buffer. since I am looking for partial matches at the end of my buffer to facilite searching a file this doesn't work. Is there some way I can get $ to not match the end of the buffer? Simplified test example below...

#include <iostream>
#include <boost/regex.hpp>

const char *buf =
R"V0G0N([2014-04-12 13:01:13.414+0100:0x00000134]: Arbitrary non matching line
[2014-04-12 13:01:14.570+0100:0x00000134]:DBLog: 'Incomplete)V0G0N";

const char *szRegex = "^\\[(.{23})(Z|[+|-][0-9]{2})[0-9x:]{11,13}\\]:DBLog: (.+)$";

int _tmain(int argc, char* argv[])
{
    char c;
    boost::regex::flag_type regex_flags = boost::regex_constants::normal | boost::regex_constants::perl;
    boost::regex_constants::match_flag_type match_flags =
        boost::regex_constants::match_default | boost::regex_constants::match_not_dot_newline | boost::regex_constants::format_perl |
        boost::regex_constants::match_partial | boost::regex_constants::match_not_eob;

    boost::regex pattern(szRegex, regex_flags);

    boost::cregex_iterator a(
        buf,
        buf + strlen(buf),
        pattern,
        match_flags);
    boost::cregex_iterator end;

    while (a != end)
    {
        if ((*a)[0].matched == false)
        {
            // Partial match, save position and break:
            std::cout << "Partial Match: " << std::string((*a)[0].first, (*a)[0].second) << std::endl;
            break;
        }
        else
        {
            std::cout << "Complete Match: " << std::string((*a)[0].first, (*a)[0].second) << std::endl;
        }
        // move to next match:
        ++a;
    }
    std::cout << "done!" << std::endl;
    std::cin >> c;
    return 0;
}

results in...

Complete Match: [2014-04-12 13:01:14.570+0100:0x00000134]:DBLog: 'Incomplete

What I need is for this to get treated as a partial match.

c++
regex
boost
asked on Stack Overflow Aug 1, 2014 by Joseph

1 Answer

0

http://www.boost.org/doc/libs/1_55_0/libs/regex/doc/html/boost_regex/ref/match_flag_type.html

on here I finally figured out that

match_not_eob

does not apply to '$'. I needed to instead use

match_not_eol
answered on Stack Overflow Aug 1, 2014 by Joseph

User contributions licensed under CC BY-SA 3.0