Regex error: Parenthesis is not closed.

1
2
3
4
5
6
7
8
9
int main()
{
     string integer = "(\+|-|\0)[0-9]([0-9]{*})";
        //string d1 = "("+integer+"."+"([0-9]{*})"+")";
        //string d2 = "("+integer+"e"+integer+")";
        //string d3 = "("+integer+"."+"([0-9]{*})e"+integer+")";
        regex pat(integer);
    return 0;
}



Can't understand what went wrong.
Last edited on
string literals are null-terminated, so `integer' see "(\+|-"
may use the constructor that specifies the length of the string
http://www.cplusplus.com/reference/string/string/string/ (5)
edit: or change the regex to "([+-]?)"

apart from that, would recommend to use raw strings to avoid backslash hell "\\+"
Last edited on
@ne555
even string integer = R"(([+-]?)[0-9]([0-9]{*}))"; fails :(
I get
Unexpected character in brace expression.


¿what's your intention with {*}?
R"(([+-]?)[0-9]([0-9]*))"
Integers with or without the sign +45,-322,12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <string>
#include <iostream>
#include <regex>
#include <algorithm>

using std::string;
using std::cin;
using std::cout;

int main()
{
    string integer = "[+-]?[0-9]+";
    std::regex pat(integer);

    string line;
    while (std::getline(cin, line)) {
	cout << line << '\t' << std::regex_match(line, pat) << '\n';
    }
}

0	1
1	1
123	1
00	1
x	0
1.2	0
+	0
-	0
+0	1
-0	1
+123	1
--4	0
+-3	0

Topic archived. No new replies allowed.