c++ - How can you specify carriage return and newline character match when using boost::regex? -
i having problem boost::regex
behaviour when comes matching \r
, \n
characters in string. communicating on serial port modem linux c++ application , receiving following message it
ati3\r\nv3.244\r\nok\r\n
i know string correct check ascii hex values of each character returned. problem application needs strip out version number specified vx.xyz
part of string. end using 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 not work - can see string correct sure making obvious error cr , nl characters in regex. have tried following not work
string str_regex("ati3.*(\d+[.]\d+).*"); string str_regex("ati3\\r\\nv(\d+[.]\d+)\\r\\nok\\r\\n");
and variations on theme think must missing basic information on how boost::regex
treats nl , cr characters. have looked through boost documentation pages without success , trying here last resort before using alternative boost solve problem.
try 1 instead :
string str_regex("ati3\r\nv(\\d+[.]\\d+)\r\nok\r\n");
notice \
of \d
escaped become \\d
.
applying same change 2 alternative regular expressions should make work.
explanation :
this not problem newline or carriage return matching, rather escape sequences in string literals. \d
not valid escape sequence string literal - in fact compiler warns me :
warning: unknown escape sequence: '\d' [enabled default]
it shortcut [:digit:]
recognized boost::regex. in order boost::regex library 'see' \
, needs escaped.
Comments
Post a Comment