Boost.Regex provides three different functions to search for regular expressions

1. regex_match

#include <boost/regex.hpp>
#include <string>
#include <iostream> int main() {
std::string s = "Boost Libraries";
boost::regex expr("\\w+\\s\\w+");
std::cout << std::boolalpha << boost::regex_match(s, expr) << std::endl;
return ;
}

boost::regex_match() compares a string with a regular expression. It will return true only if the expression matches the complete string.

2. regex_search

#include <boost/regex.hpp>
#include <string>
#include <iostream> int main() {
std::string s = "Boost Libraries";
boost::regex expr("(\\w+)\\s(\\w+)");
boost::smatch what;
if (boost::regex_search(s, what, expr)) {
std::cout << what[] << std::endl;
std::cout << what[] << "_" << what[] << std::endl;
}
return ;
}

3. regex_replace

#include <boost/regex.hpp>
#include <string>
#include <iostream> int main() {
std::string s = " Boost Libraries";
boost::regex expr("\\s");
std::string fmt("_");
std::cout << boost::regex_replace(s, expr, fmt) << std::endl;
return ;
}

4. boost xpressive

Like boost regex, boost xpressive provides functions to search strings using regular expressions. However, boost xpressive makes it possible to write down regular expressions as C++ code rather than strings. That makes it possible to check at compile time whether a regular expression is valid or not.

#include <boost/xpressive/xpressive.hpp>
#include <string>
#include <iostream> using namespace boost::xpressive; int main() {
std::string s = "Boost Libraries";
sregex expr = sregex::compile("\\w+\\s\\w+");
std::cout << std::boolalpha << regex_match(s, expr) << std::endl;
return ;
}

boost::xpressive::regex_match() compares strings, boost::xpressive::regex_search() searches in strings, and boost::xpressive::regex_replace() replaces characters in strings. The type of regular expression in boost xpressive depends on the type of string being searched. Because s is based on std::string, the type of the regular expression must be boost::xpressive::sregex.

#include <boost/xpressive/xpressive.hpp>
#include <iostream> using namespace boost::xpressive; int main() {
const char* c = "Boost Libraries";
cregex expr = cregex::compile("\\w+\\s\\w+");
std::cout << std::boolalpha << regex_match(c, expr) << std::endl;
return ;
}

For strings of type const char*, use the class boost::xpressive::cregex.

boost regex expression的更多相关文章

  1. 使用Boost Regex 的regex_search进行遍历搜索

    在regex_search函数中,会将找到的第一个匹配结果保存到一个smatch类中. 然而如果搜索字符串中有多个匹配结果,则需要自己实现了. 在smatch中,有两个成员,官方文档如下: itera ...

  2. c++ 使用boost regex库 总结

    用java的时候觉得挺折腾,回头来弄c++才知道什么叫折腾...汗... 首先参考我写的这篇文章:http://www.cnblogs.com/qrlozte/p/4100892.html 我从sou ...

  3. vs 2005 使用 boost regex

    第一步: Boost 入门及其VS2005下编译boost库  boost.regex库安装指南  深入浅出之正则表达式(一)  C++中三种正则表达式比较(C regex,C ++regex,boo ...

  4. #include <boost/regex.hpp>

    boost C++的正则表达式库boost.regex可以应用正则表达式于C++.正则表达式大大减轻了搜索特定模式字符串的负担,在很多语言中都是强大的功能. boost.regex库中两个最重要的类是 ...

  5. 解决Boost.Regex对中文支持不好的问题

    解决Boost.Regex对中文支持不好的问题 - k.m.Cao - 博客频道 - CSDN.NET 解决Boost.Regex对中文支持不好的问题 k.m.Caov0.1   问题的提出: Boo ...

  6. boost::string or boost::regex

    有时候写代码时会遇到下面问题 如果有一个文本文件,其包括内容类似于C语言,当中有一行例如以下格式的语句: layout (local_size_x = a,local_size_y = b, loca ...

  7. boost:regex分割字符串(带有'\'字符) - zzusimon的专栏 - 博客频道 - CSDN.NET

    boost:regex分割字符串(带有'\'字符) - zzusimon的专栏 - 博客频道 - CSDN.NET boost:regex分割字符串(带有'\'字符) 分类: C++ 2011-08- ...

  8. libraries\include\boost-1_61\boost/regex/v4/perl_matcher.hpp(362): error C2292: 'boost::re_detail_106100::perl_matcher<const char *,std::allocator<boost::sub_match<const char *>>,boost::regex_traits<c

    这个问题在Windows上基于CMake编译Caffe-SSD的GPU版时出现. 网上找到的博客贴出的解决办法是删掉regex和rv相关代码,甚至不编译detection_output_layer.c ...

  9. profile对比std::regex与boost::regex的性能

    c++11标准库的regex比boost库的regex之间的性能差距接近5倍,这是为什么?stackflow上也找到一篇post<c++11 regex slower than python&g ...

随机推荐

  1. 【leetcode】564. Find the Closest Palindrome

    题目如下: 解题思路:既然是要求回文字符串,那么最终的输出结果就是对称的.要变成对称字符串,只要把处于对称位置上对应的两个字符中较大的那个变成较小的那个即可,假设n=1234,1和4对称所以把4变成1 ...

  2. C++ 彩色图像(RGB)三通道直方图计算和绘制,图像逆时针旋转90° 实现代码

    #include "iostream" #include "opencv2/opencv.hpp" #include "vector" us ...

  3. springMVC使用map接收入参 + mybatis使用map 传入查询参数

    测试例子: controllel层 ,使用map接收请求参数,通过Debug可以看到,请求中的参数的值都是字符串形式,如果将这个接收参数的map直接传入service,mybatis接收参数时会报错, ...

  4. bootstrap 点击模态框上的提交按钮后,模态框不能关闭的解决办法

    项目问题如下图, 点击确定后,模态框没反应,按理,点击删除按钮时,弹出确认删除的模态框,点击确定后,使用ajax请求服务器,把数据库中对应的数据进行删除,根据服务器 servlet返回的状态值(del ...

  5. php pi()函数 语法

    php pi()函数 语法 pi()函数是什么意思? php pi()函数用于获取圆周率值,语法是pi(),这个函数只是单纯的用来获取圆周率值深圳大理石平台 作用:获取圆周率值 语法:pi() 参数: ...

  6. [bzoj3462]DZY Loves Math II (美妙数学+背包dp)

    Description Input 第一行,两个正整数 S 和 q,q 表示询问数量. 接下来 q 行,每行一个正整数 n. Output 输出共 q 行,分别为每个询问的答案. Sample Inp ...

  7. B-Tree, B+Tree, B*树介绍

    [数据结构]B-Tree, B+Tree, B*树介绍 转   [数据结构]B-Tree, B+Tree, B*树介绍 [摘要] 最近在看Mysql的存储引擎中索引的优化,神马是索引,支持啥索引.全是 ...

  8. Caused by: java.lang.ClassNotFoundException: com.mchange.v2.cfg.MConfig

    出错原因:c3p0 为0.9.5.2版本 而使用了 mchange-commons-java 的版本为0.2.3.4,mchange-commons-java 的版本太高了, 将mchange-com ...

  9. wxparse使用(富文本插件)

    优点:目前已知唯一可以转化HTML到小程序识别的插件 缺点:转换一个HTML标签可能需要大量的微信小程序标签还有样式 配置:第一步,下载 https://github.com/icindy/wxPar ...

  10. OpenCV2.4.8 + CUDA7.5 + VS2013 配置

    配置过程主要参考:https://initialneil.wordpress.com/2014/09/25/opencv-2-4-9-cuda-6-5-visual-studio-2013/ 1.为什 ...