You are not logged in.

#1 2013-05-02 20:13:56

ypoluektovich
Member
Registered: 2011-11-10
Posts: 13

[solved-ish] C++11 regular expressions

Can somebody please explain to me why this code produces these results?

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

int main (int argc, char** argv) {
    std::regex exp;
    try {
        exp = std::regex("[0-9].*", std::regex_constants::basic);
    } catch (const std::regex_error& e) {
        std::cerr << "Error: " << e.what() << std::endl;
        std::cerr << "Error code: " << e.code() << std::endl;
        return 1;
    }
    std::cout << "created regex object" << std::endl;

    for (int i = 1; i < argc; ++i) {
        std::string inputString(argv[i]);
        std::cout << "input string: '" << inputString << "'" << std::endl;
        std::cout << (std::regex_match(inputString, exp) ? "matches" : "doesn't match") << std::endl;
    }
    return 0;
}
$ g++ -std=c++11 x.cpp 

$ ./a.out 1 11 a 1a a1 aa
created regex object
input string: '1'
matches
input string: '11'
doesn't match
input string: 'a'
matches
input string: '1a'
doesn't match
input string: 'a1'
doesn't match
input string: 'aa'
doesn't match

Last edited by ypoluektovich (2013-05-03 11:57:07)

Offline

#2 2013-05-03 11:56:41

ypoluektovich
Member
Registered: 2011-11-10
Posts: 13

Re: [solved-ish] C++11 regular expressions

Eh, the answer is actually pretty disappointing.

http://gcc.gnu.org/onlinedocs/libstdc++ … s.iso.2011

Currently, almost all the regular expression stuff is not implemented yet. I'll have to use boost regex.

Offline

#3 2013-05-08 10:37:33

snack
Member
From: Italy
Registered: 2009-01-13
Posts: 671

Re: [solved-ish] C++11 regular expressions

Or try with LLVM/Clang, they claim full C++11 support.

Offline

#4 2013-07-15 12:31:56

mthinkcpp
Member
Registered: 2013-05-11
Posts: 23

Re: [solved-ish] C++11 regular expressions

snack wrote:

Or try with LLVM/Clang, they claim full C++11 support.

Just using Clang does not provide regular expression support. You need to use use clang and libc++ for that (both found in the official repositories).

Then you programme using the regex library as you would expect it to work.

Note: compile your programmes with:

clang++ -stdlib=libc++ -std=c++11 -lc++abi my_file.cpp

Clang supports all the common gcc options so any Makefiles or build systems will still function upon switching.

Last edited by mthinkcpp (2013-07-15 12:32:13)

Offline

Board footer

Powered by FluxBB