boost xpressive sregex の割り当てで奇妙な動作に気付きました。以下のコードを参照してください。機能しない最初のコード スニペットは、sregex にオブジェクトの予備的な割り当てがあり、後でメインの式で使用されます。うまく機能する 2 番目のコード スニペットには、以前の sregex 割り当てがありません (最後のメインのものを除く)。ブースト xpressive api を間違って使用している場合はお知らせください。
動作しないコード
mark_tag Value1(1), Value2(2), Value3(3), Value4(4), Value5(5), Value6(6), Value7(7);
boost::xpressive::sregex name,multicast,rtsp;
name = ( (Value1 = (+boost::xpressive::set[_w|_d|'-'|'_'|as_xpr(' ')]) ) >> ',' );
name1 =
( (Value2 = icase(as_xpr("mark1:") ) )
>> (Value3 = (+boost::xpressive::set[_d|'.']) )
>> ':'
>> (Value4 = (+boost::xpressive::set[_d]) ) >> optional(as_xpr(",")) );
name2 =
( (Value5 = icase(as_xpr("mark2:") ) )
>> (Value6 = (+boost::xpressive::set[_d|'.']) )
>> ':'
>> (Value7 = (+boost::xpressive::set[_d]) ) >> optional(as_xpr(",")) ) ;
boost::xpressive::sregex pt = bos
>> (
name
>> repeat<0,2>(
name1
|
name2)
)
>> eos;
boost::trim(string_to_parse);
smatch what;
if ( !regex_search(string_to_parse, what, pt)) {
std::stringstream ss;
ss << "Unable to parse: " << string_to_parse;
throw parse::MyException(ss.str());
}
std::string Value1_str = what[Value1]; // print them later
std::string Value2_str = what[Value2]; // print them later
std::string Value3_str = what[Value3]; // print them later
std::string Value4_str = what[Value4]; // print them later
std::string Value5_str = what[Value5]; // print them later
std::string Value6_str = what[Value6]; // print them later
std::string Value7_str = what[Value7]; // print them later
string_to_parse = NameX,mark1:192.168.1.100:5555,mark2:192.168.1.101:5556;
(解析に失敗) what[<>] には値が含まれていないことを意味します。
動作するコード
mark_tag Value1(1), Value2(2), Value3(3), Value4(4), Value5(5), Value6(6), Value7(7);
sregex pt = bos
>> (
( (Value1 = (+boost::xpressive::set[_w|_d|'-'|'_'|as_xpr(' ')]) ) >> ',' )
>> repeat<0,2>(
( (Value2 = icase(as_xpr("mark1:") ) ) >> (Value3 = (+boost::xpressive::set[_d|'.']) ) >> ':' >> (Value4 = (+boost::xpressive::set[_d]) ) >> optional(as_xpr(",")) )
|
( (Value5 = icase(as_xpr("mark2:") ) ) >> (Value6 = (+boost::xpressive::set[_d|'.']) ) >> ':' >> (Value7 = (+boost::xpressive::set[_d]) ) >> optional(as_xpr(",")) ) )
)
>> eos;
boost::trim(string_to_parse);
smatch what;
if ( !regex_search(string_to_parse, what, pt)) {
std::stringstream ss;
ss << "Unable to parse: " << string_to_parse;
throw parse::MyException(ss.str());
}
std::string Value1_str = what[Value1]; // print them later
std::string Value2_str = what[Value2]; // print them later
std::string Value3_str = what[Value3]; // print them later
std::string Value4_str = what[Value4]; // print them later
std::string Value5_str = what[Value5]; // print them later
std::string Value6_str = what[Value6]; // print them later
std::string Value7_str = what[Value7]; // print them later
string_to_parse = NameX,mark1:192.168.1.100:5555,mark2:192.168.1.101:5556;
(パースに合格)