小编eth*_*du7的帖子

提升正则表达式模式语法混乱

我正在使用boost regex,但我对boost regex使用的语法感到困惑.如果我想使用模式"bb"匹配字符串"aabbcc",我必须将模式"bb"设置为".*bb.*",以便匹配字符串.这是有线的,因为在perl中你不需要在"bb"的前端和末尾添加".*".我是否错过了关于boost regex的一些内容,或者这只是boost regex的味道?以下是此问题的简单源代码:

#include <boost/regex.hpp>
#include <iostream>
#include <string>
int main() {
    boost::regex regex_bb01("bb");
    boost::regex regex_bb02(".*bb");
    boost::regex regex_bb03("bb.*");
    boost::regex regex_bb04(".*bb.*");

    if(boost::regex_match("aabbcc", regex_bb01))
        std::cout<<"the regex_bb01 is matched\n";
    else
        std::cout<<"the regex_bb01 is Not matched\n";
    if(boost::regex_match("aabbcc", regex_bb02))
        std::cout<<"the regex_bb02 is matched\n";
    else
        std::cout<<"the regex_bb02 is Not matched\n";
    if(boost::regex_match("aabbcc", regex_bb03))
        std::cout<<"the regex_bb03 is matched\n";
    else
        std::cout<<"the regex_bb03 is Not matched\n";
    if(boost::regex_match("aabbcc", regex_bb04))
        std::cout<<"the regex_bb04 is matched\n";
    else
        std::cout<<"the regex_bb04 is Not matched\n";
}
Run Code Online (Sandbox Code Playgroud)

结果如下:

[root @ localhost BoostCase]#./ regex_test

regex_bb01不匹配

regex_bb02不匹配

regex_bb03不匹配 …

c++ regex perl boost

2
推荐指数
1
解决办法
117
查看次数

标签 统计

boost ×1

c++ ×1

perl ×1

regex ×1