1

Today I encountered strange bug in my application. I've tested it for like 2 hours and didn't find a solution. Maybe you can help me solving this problem. So here it is:

#include <iostream>
#include <regex>
#include <vector>

int main()
{
        std::regex reg("rmvb|avi|rm|mp4|256");

        std::vector<std::string> ext{"rmvb", "avi", "rm", "mp4", "256", "null"};

        for (int i = 0; i < 6; i++)
        {
                std::cout << ext[i] << "\t" << std::boolalpha << std::regex_match(ext[i], reg) << std::endl;
        }

        return 0;
}

Output:

rmvb    true
avi true
rm  false
mp4 false
256 false
null    false

It seems that pattern is discarded after the second element - no matter what order I choose (I tried to swap them, because I thought that digits can cause this bug - but they aren't). Now I have no idea what's going on.

I'm using gcc version 4.6.3 (Debian 4.6.3-1).

Kacper Banasik
  • 191
  • 3
  • 13
  • On my gcc 4.6.2, I can't even make the code compile properly. `std::regex` is part of TR1, so it should be `#include `, `std::tr1::regex`, and `std::tr1::regex_match`. Once that's out of the way, it won't link: I get the error from http://stackoverflow.com/questions/2860722/linker-error-when-using-tr1regex when using `g++-4.6.2 -std=c++0x foo.cc`. Could you possibly give some more details on how the code runs at all in your environment? – Borealid Mar 19 '12 at 02:37
  • P.S. problem is almost certainly the vector initializer. I don't know enough about C++0x to say what the code you've written does, but that's the suspicious part. Perhaps you should try building the vector the old-fashioned way and see if that makes it work? – Borealid Mar 19 '12 at 02:40
  • @Borealid: It's ok. When compiling with `-std=c++0x`, `std::regex` does live in ``. The vector initializer is also ok (see how it prints the contents correctly). – R. Martinho Fernandes Mar 19 '12 at 02:42

1 Answers1

3

The regex library is mostly not yet implemented in libstc++ (cf. status page). This may be a bug or just the result of it not being implemented. I'd suggest using Boost.Regex as a replacement.

ildjarn
  • 62,044
  • 9
  • 127
  • 211
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
  • 1
    Personally, unless I needed the ICU support, I would always opt for [Boost.Xpressive](http://www.boost.org/libs/xpressive/) over Boost.Regex. – ildjarn Mar 19 '12 at 04:28