c++ - Boost Xpressive sregex 分配和捕获组问题

标签 c++ boost boost-xpressive

我注意到 boost xpressive sregex 赋值中有奇怪的行为。请参阅下面的代码。第一个不起作用的代码片段是 sregex 具有对象初步分配,然后在主表达式中使用。第二个运行良好的代码片段没有先前的 sregex 分配(除了最后一个主要的)。如果我错误地使用了 boost 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;(解析失败) 含义[<>]不包含的内容任何值。

有效的代码

   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;(通过解析)

最佳答案

当您将模式与嵌套正则表达式匹配时,您将获得嵌套匹配结果。 This解释了这一切。

关于c++ - Boost Xpressive sregex 分配和捕获组问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16465628/

相关文章:

c++ - 标记一个字符串,不包括引号内的定界符

c++ - 以编程方式调用 "Save to PDF..."工作表

c++ - Boost C 正则表达式 - 如何返回所有匹配项

c++ - 在 Windows VC++ 2010 静态链接下构建 boost + ICU

c++ - 尝试在 cmake 项目中包含 boost 后出现链接错误

c++ - boost 表现力!接线员不工作

c++ - 正则表达式:boost::xpressive 与 boost::regex

java - 安装定制的共享库文件/或其任何替代方案

c++ - 带 ESP8266 的 PMS5003 - 许多校验和错误

c++ - 名称隐藏和访问基类的非虚函数(语法)