3

I am trying the C++ standard regex library. But it is throwing std::regex_error exception. No idea what is going wrong. I can use Boost regex or PCRE also.

But I thought of going with G++ provided regex module. But unable to make it work with even simple expressions. Don't worry about the unused variables, I started with a moderately complex expression which needed them. But now I have reduced it just to match a word.

Please check sample which is crashing here:

http://ideone.com/fork/58EV7v

#include <iostream>
#include <stdio.h>
#include <regex>

int main()
{
    const char *vals = "abcdef";
    unsigned long start;
    unsigned int off;
    unsigned int size;

    std::cmatch cr;
    std::regex rx("(\\w+)");
    std::regex_match(vals, cr, rx);
    std::cmatch::iterator it = cr.begin();
    while (it != cr.end()){
        std::cout << *it << std::endl;
        ++it;
    }

    return 0;
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
Nipun Talukdar
  • 4,975
  • 6
  • 30
  • 42
  • +1 for providing an [SSCCE](http://sscce.org/) and explaining the problem clearly. – Amal Murali Sep 10 '14 at 16:28
  • 1
    Are you perhaps using GCC v4.8? It has a *limited* regex implementation, to say the least. – Biffen Sep 10 '14 at 16:29
  • 2
    ideone is using GCC 4.8, which doesn't implement regex. – interjay Sep 10 '14 at 16:39
  • I'm running 4.9.1 and it works. – Carl Sep 10 '14 at 16:40
  • Yes. I am using GCC 4.8.1. IDEONE is also using GCC 4.8. But it was crashing with GCC 4.6 as well. – Nipun Talukdar Sep 10 '14 at 16:41
  • @NipunTalukdar GCC v4.6 doesn't have a better regex implementation than v4.8. If you want to use `` I suggest moving to v4.9, or some other compiler, e.g. Clang. – Biffen Sep 10 '14 at 16:43
  • What `throwing std::regex_error exception` are you getting, or is this just a compiler issue/question? –  Sep 10 '14 at 16:44
  • 1
    @Biffen I have to use GCC 4.6 as that is what we use for building C++ modules that goes into production. Anyway I will go with Boost.Regex. – Nipun Talukdar Sep 10 '14 at 16:48
  • @sln Exception is thrown from regex_match (i.e. from the line std::regex_match(vals, cr, rx)) – Nipun Talukdar Sep 10 '14 at 16:51
  • You can see here that it will not work with 4.8.1: http://ideone.com/iG0cS9 However, what I did was compile a regex class into a .dll/.so, with visual studio and/or gcc 4.8.1 on linux and used that.. Worked like a charm. – Brandon Sep 10 '14 at 16:54

0 Answers0