Boost表达式sregex分配和捕获组问题
Boost Xpressive sregex assignment and capture group issue
我注意到boost表达式sregex赋值中的奇怪行为。请参阅下面的代码。第一个不起作用的代码片段具有sregex对象初始赋值,然后在主表达式中使用。第二个运行良好的代码片段没有任何先前的sregex赋值(除了最后一个主代码)。请让我知道,如果我使用boost表达式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;
(failed parsing)表示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;
(通过解析)
当您用嵌套的正则表达式匹配模式时,您将得到嵌套的匹配结果。
相关文章:
- gcc和clang在表达式是否为常量求值的问题上存在分歧
- 在 C++ 中使用正则表达式错误时出现问题 括号表达式中的范围无效
- 堆栈问题(平衡表达式问题集)
- 生成提升::hana::set 的常量表达式问题
- 关于在需要常量表达式的上下文中使用的glvalue常量表达式的问题
- 二进制表达式 cout 问题的无效操作数
- 我的作业问题:布尔表达式
- C++中的正则表达式存在问题
- 令牌问题之前'*'预期的主表达式
- 关于Visual Studio 2017中有关ConstexPR表达式的汇编问题
- CPP 中的正则表达式问题
- 有关特定表达式的C/C++中宏定义的问题
- 非常基本的C++问题(预期表达式)
- 这种使用增强正则表达式库有什么问题
- regex_error。我的正则表达式有什么问题
- c++矩阵与表达式模板问题
- #include < regex.h> 用于创建正确正则表达式的问题
- gcc 4.8或更早版本对正则表达式有问题吗
- 二进制表达式的语义问题无效操作数("双精度"和"双精度")
- 非常基本的C++程序问题 - 二进制表达式的操作数无效