gpt4 book ai didi

c++ - boost 正则表达式模式语法困惑

转载 作者:搜寻专家 更新时间:2023-10-31 00:31:04 25 4
gpt4 key购买 nike

我正在使用 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";
}

结果是这样的:

[root@localhost BoostCase]# ./regex_test

regex_bb01 不匹配

regex_bb02 不匹配

regex_bb03 不匹配

regex_bb04 匹配

最佳答案

来自 boost 文档中的函数 regex_match

The algorithm regex_match determines whether a given regular expression matches all of a given character sequence denoted by a pair of bidirectional-iterators, the algorithm is defined as follows, the main use of this function is data input validation.

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

关于c++ - boost 正则表达式模式语法困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34983344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com