regex error with valid regex string
Nov 6, 2013 at 7:44pm UTC
I've tested the string and found it to be valid (as it should be) and it matches in perl, but in C++ it throws a regex_error with code 4. For the life of me I can't figure out why. I'm new to regex, and my googling has been fruitless.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <regex>
#include <string>
using namespace std;
int main() {
try {
regex rgx("(^[A-Za-z])" );
smatch match;
string name = "Alex Evans" ;
if (std::regex_search(name,match,rgx)) {
cout << "match[1] = " << match[1] << endl;
}
} catch (std::regex_error e) {
cout << e.code() << endl;
}
return 0;
}
Nov 6, 2013 at 8:26pm UTC
Are you using the GNU compiler (release prior to [2013-10-03])?
Nov 6, 2013 at 8:29pm UTC
GCC 4.8.1
Nov 6, 2013 at 8:32pm UTC
Regex support is not available
Upgrade to GCC 4.8.2 (haven't tested that version myself, but I guess it would have regex support.)
Or use boost::regex instead.
Nov 6, 2013 at 8:47pm UTC
Unfortunately that's not possible at the moment. Well crap. I guess I'll go at it again later.
I hate that it compiles perfectly fine as is, though.
Nov 6, 2013 at 8:55pm UTC
4.8.2 doesn't have it yet, it's in the 4.9 trunk
Nov 6, 2013 at 11:45pm UTC
The GNU C library has long had a <regex.h>, which is very useful.
Give it a try.
Topic archived. No new replies allowed.