terminate called after throwing an instance of 'std::regex_error' what(): regex_error

Hello!

I run this program and receive this message:
Invalid parameter passed to C runtime function.
terminate called after throwing an instance of 'std::regex_error'
what(): regex_error


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// regex_match_dates_1.cpp
#include <iostream>
#include <string>
#include <regex>

using namespace std;

int main()
{
    regex r("^\\d{4}/(?:0?[1-9]|1[0-2])/(?:0?[1-9]|[1-2][0-9]|3[0-1])$");

    while (true) {
        cout << "Enter a date (year/month/day) (q=quit): ";
        string str;
        if (!getline(cin, str) || str == "q")
            break;
        if (regex_match(str, r))
            cout << "  Valid date." << endl;
        else
            cout << "  Invalid date!" << endl;
    }

    return 0;
}


Thank you for help in advance!

P.S. I use Qt Creator IDE.
1) You have two unescaped delmiters here (/)
2) AFAIK QtCreator uses GCC which is not fully supports regex until 4.9
'/' isn't a delimiter, it's a regular single-character regex atom

The program works as-is with clang++/libc++ (which implemented std::regex): http://coliru.stacked-crooked.com/a/2be9436e8bd8e2e7

gcc 4.8 indeed throws a regex_error, as MiiNiPaa correctly pointed out, it does not support std::regex. Either upgrade to 4.9 or switch to boost.regex.
Topic archived. No new replies allowed.