How can you specify carriage return and newline character match when using boost::regex?

StackOverflow https://stackoverflow.com/questions/17854035

  •  04-06-2022
  •  | 
  •  

Вопрос

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.

Это было полезно?

Решение

Try with this one instead :

string str_regex("ATI3\r\nv(\\d+[.]\\d+)\r\nOK\r\n");

Notice that the \ of \d is escaped to become \\d.

Applying the same change to your two alternative regular expressions should also make those work.

Explanation :

This is not a problem with the newline or carriage return matching, but rather with escape sequences in string literals. \d is not a valid escape sequence for a string literal - in fact my compiler warns me about this :

warning: unknown escape sequence: '\d' [enabled by default]

It is a shortcut for [:digit:] recognized by boost::regex. In order for the boost::regex library to 'see' the \, it needs to be escaped.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top