I am having a problem with boost::regex
behaviour when it comes to matching \r
and \n
characters in a string. I am communicating over a serial port with a modem from my linux C++ application and I am receiving the following message from it
ATI3\r\nv3.244\r\nOK\r\n
I know that this string is correct as I actually check the ASCII hex values of each character returned. The problem is that my application needs to strip out the version number specified by the vX.XYZ
part of the string. To this end I am using the following boost::regex
based code:
string str_modem_fw_version_number = "";
string str_regex("ATI3\r\nv(\d+[.]\d+)\r\nOK\r\n");
boost::regex patt;
try
{
patt.assign(str_regex);
boost::cmatch what;
if (boost::regex_match(str_reply.c_str(), sc_what, patt)) {
str_modem_fw_version_number = string(sc_what[1].first,sc_what[1].second);
}
}
catch (const boost::regex_error& e)
{
cout << e.what() << endl;
}
The above does not work - I can see the string I get back is correct but I am sure I am making some obvious error with the CR and NL characters in the regex. I have also tried the following which do not work
string str_regex("ATI3.*(\d+[.]\d+).*");
string str_regex("ATI3\\r\\nv(\d+[.]\d+)\\r\\nOK\\r\\n");
and variations on a theme but I think I must be missing some basic information on how boost::regex
treats the NL and CR characters. I have looked through the boost documentation pages without success and so am trying here as a last resort before using an alternative to boost to solve the problem.