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. Echarts--Y坐标标题显示不全

    如:下图,Y坐标标题显示不全 y2可以控制不显示区域的高度,就能显示全啦 grid:{ x:40, x2:100, y2:200 }

  2. FMXUI ANDROID下连续按多次返回出现异常

    在ANDROID下,按返回键后,默认是关闭当前Frame,但连接按返回键,会对当前Frame执行多次关闭动作,​​因为已经释放过对象,再次关闭会出现异常错误,​解决办法:定义一个标识如FClosed: ...

  3. Android中插件开发篇总结和概述

    刚刚终于写完了插件开发的最后一篇文章,下面就来总结一下,关于Android中插件篇从去年的11月份就开始规划了,主要从三个方面去解读Android中插件开发原理.说白了,插件开发的原理就是:动态加载技 ...

  4. 汇编 “error A2031”

    assume cs:code code segment main: mov ax,0020h mov ds,ax ;指定段地址0200h mov dx,0h mov cx,003fh s: mov [ ...

  5. python数据分析中常用的库

    Python是数据处理常用工具,可以处理数量级从几K至几T不等的数据,具有较高的开发效率和可维护性,还具有较强的通用性和跨平台性,这里就为大家分享几个不错的数据分析工具,需要的朋友可以参考下 Pyth ...

  6. python中将12345转换为'12345',不要使用str

    a = 12345 #创建一个空字符串 ret = "" #whlie循环,条件为当a为true时,即a不是 0的时候 while a : #定义一个变量,对a求余 last = ...

  7. xcode-select: error: tool 'xcodebuild' requires Xcode, but active developer directory '/Library/Developer/CommandLineTools' is a command line tools instance

    运行xcode命令报错: sh-3.2# xcodebuild xcode-select: error: tool 'xcodebuild' requires Xcode, but active de ...

  8. XAMPP【phpmyadmin】外网访问被拒绝解决办法

    问题场景: 在阿里云搭建一个apache服务器,正常访问XAMPP目录下的页面. 服务器本地是可以正常访问的 但是远程 就不可以访问了: 出现这样的画面: 解决方法 1.按照提示找到httpd-xam ...

  9. 洛谷P3366 【模板】最小生成树(LCT)

    [模板]最小生成树 题目传送门 解题思路 用LCT来维护最小生成树. 除了把各顶点作为节点外,每条边也都视为一个节点.对于要加入的边\(e\),检查其两顶点\(x\)和\(y\)是否在同一棵树中,如果 ...

  10. upc组队赛14 Communication【并查集+floyd /Tarjan】

    Communication 题目描述 The Ministry of Communication has an extremely wonderful message system, designed ...