Reputation: 237
I came accross a very strange problem with tr1::regex (VS2008) that I can't figure out the reason for. The code at the end of the post compiles fine but throws an exception when reaching the 4th regular expression definition during execution:
Microsoft C++ exception: std::tr1::regex_error at memory location 0x0012f5f4..
However, the only difference I can see (maybe I am blind) between the 3rd and 4th one is the 'NumberOfComponents' instead of 'SchemeVersion'. At first I thought maybe both (3rd and 4th) are wrong and the error from the 3rd is just triggered in the 4th. That seems not to be the case as I moved both of them around and put multiple other regex definitions between them. The line in question always triggers the exception.
Does anyone have any idea why that line
std::tr1::regex rxNumberOfComponents("\\NumberOfComponents:(\\s*\\d+){1}");
triggers an exception but
std::tr1::regex rxSchemeVersion("\\SchemeVersion:(\\s*\\d+){1}");
doesn't? Is the runtime just messing with me?
Thanks for the time to read this and for any insights.
T
PS: I am totally sure the solution is so easy I have to hit my head against the nearest wall to even out the 'stupid question' karma ...
#include <regex>
int main(void)
{
std::tr1::regex rxSepFileIdent("Scanner Separation Configuration");
std::tr1::regex rxScannerNameIdent("\\ScannerName:((\\s*\\w+)+)");
std::tr1::regex rxSchemeVersion("\\SchemeVersion:(\\s*\\d+){1}");
std::tr1::regex rxNumberOfComponents("\\NumberOfComponents:(\\s*\\d+){1}");
std::tr1::regex rxConfigStartIdent("Configuration Start");
std::tr1::regex rxConfigEndIdent("Configuration End");
return 0;
}
Upvotes: 1
Views: 2414
Reputation: 527233
You need to double-escape your backslashes - once for the regex itself, a second time for the string they're in.
The one that starts with S works because \S
is a valid regex escape (non-whitespace characters). The one that starts with N does not (because \N
is not a valid regex escape).
Instead, use "\\\\SchemeVersion:
et cetera.
Upvotes: 2