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

eth*_*du7 2 c++ regex perl boost

我正在使用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不匹配

regex_bb04匹配

小智 5

从boost文档中可以看到函数regex_match

算法regex_match确定给定的正则表达式是否匹配由一对双向迭代器表示的所有给定字符序列,该算法定义如下,该函数的主要用途是数据输入验证.

如果你想使用'bb'匹配,你需要使用boost :: regex_search