0

For whatever reason, the regexp I made for my program will not match to lines that match in any other implementation. I'm wondering if there's some new syntax I'm not understanding?

Here's my own implementation:

std::string line, pattern("[LS] 0x[0-9a-fA-F]{8}");
std::regex rx(pattern, std::regex::basic);
uint line_count = 0;

while (getline(in_file, line))
{
    line_count++;

    if (std::regex_match(line, rx))
    {
        bool type = (line[0] == 'L');
        uint address = (uint) std::stoul(line.substr(4, 8), nullptr, 16);

        m_type_store.push_back(type);
        m_address_store.push_back(address);
    } else {
        std::cerr << line << " |" << line_count << '|' << std::endl;
        in_file.close();
        exit(1);
    }
}

And every time it cannot match the first line of the file, which is exactly:

S 0x0022f5b4

And that looks completely fine to me... Am I missing something?

Brian Rodriguez
  • 487
  • 3
  • 10
  • 4
    Please notice: g++ broken implementation: http://stackoverflow.com/questions/12530406/is-gcc4-7-buggy-about-regular-expressions –  Apr 29 '14 at 15:23
  • My only suggestions for improvement would be to provide a complete main function that others can copy and paste easily. Also what implementations are you comparing to? You also haven't stated what compiler you are using. – shawn1874 Apr 29 '14 at 15:36
  • I tried that regex with the Faust Regex explorer addin for visual studio. The expression worked fine for me except that you have not anchored it with '^'. Perhaps that was by design. I tested a sample program with GCC 4.7.2 and it did not work, so if you are using GCC then the link above is probably the answer. http://www.compileonline.com/compile_cpp11_online.php – shawn1874 Apr 29 '14 at 15:46
  • 2
    @DieterLücking just for the record, `libstdc++ 4.8` is equally buggy. OTOH, 4.9 brought us working ``. But who has access only to `gcc <= 4.8` or does not has access to `libc++` has to resort to `boost::regex` instead... – Massa Apr 29 '14 at 15:53

1 Answers1

0

In your code, you are using Basic regular expression. I'm not sure about C++, but normally in Basic RE, you need to prefix the { and } with a \ when specifying multiplicity. In other words, your code should be:

std::string line, pattern("[LS] 0x[0-9a-fA-F]\{8\}");

Alternatively, you can use Extended RE instead of Basic RE, as in std::regex::extended.

SF Lee
  • 1,767
  • 4
  • 17
  • 32