rhobincu
rhobincu

Reputation: 926

C++11 match_regex doesn't match a simple pattern

I've been having some trouble with the new regular expression library in C++. Here's a simple example:

#include<regex>
#include<string>
#include<iostream>

using namespace std;

int main(){
    string text = "123.456";
    string pattern = "[0-9]+\\.[0-9]+";
    try{
        cout << (regex_match(text, regex(pattern, regex_constants::extended)) ? "Pass\n" : "Fail\n");
    }catch(...){
        cout << "Fail (bad regex)\n";
    }
    return 0;
}

The problem is, no matter what type of matching I use (simple, extended, grep, egrep, awk, etc.), it always returns false. If I use "regex_constants::simple" it throws an exception, because bracketed expressions are not supported, but I checked the specs and it should work fine with "regex_constants::extended".

This is the result:

rhobincu@daneel:~/work$ g++ -std=c++11 test.cpp -o test
rhobincu@daneel:~/work$ ./test 
Fail

Any idea what I'm doing wrong?

EDIT: This might be useful info as well:

rhobincu@daneel:~/work$ g++ --version
g++ (Ubuntu/Linaro 4.8.1-10ubuntu8) 4.8.1
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Upvotes: 0

Views: 337

Answers (1)

v154c1
v154c1

Reputation: 1698

regex is not yet supported in GCC's libstdc++, current status.

You can just replace std::regex with boost::regex and it compiles and works fine.

Upvotes: 6

Related Questions