boost regex expression
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的更多相关文章
- 使用Boost Regex 的regex_search进行遍历搜索
在regex_search函数中,会将找到的第一个匹配结果保存到一个smatch类中. 然而如果搜索字符串中有多个匹配结果,则需要自己实现了. 在smatch中,有两个成员,官方文档如下: itera ...
- c++ 使用boost regex库 总结
用java的时候觉得挺折腾,回头来弄c++才知道什么叫折腾...汗... 首先参考我写的这篇文章:http://www.cnblogs.com/qrlozte/p/4100892.html 我从sou ...
- vs 2005 使用 boost regex
第一步: Boost 入门及其VS2005下编译boost库 boost.regex库安装指南 深入浅出之正则表达式(一) C++中三种正则表达式比较(C regex,C ++regex,boo ...
- #include <boost/regex.hpp>
boost C++的正则表达式库boost.regex可以应用正则表达式于C++.正则表达式大大减轻了搜索特定模式字符串的负担,在很多语言中都是强大的功能. boost.regex库中两个最重要的类是 ...
- 解决Boost.Regex对中文支持不好的问题
解决Boost.Regex对中文支持不好的问题 - k.m.Cao - 博客频道 - CSDN.NET 解决Boost.Regex对中文支持不好的问题 k.m.Caov0.1 问题的提出: Boo ...
- boost::string or boost::regex
有时候写代码时会遇到下面问题 如果有一个文本文件,其包括内容类似于C语言,当中有一行例如以下格式的语句: layout (local_size_x = a,local_size_y = b, loca ...
- boost:regex分割字符串(带有'\'字符) - zzusimon的专栏 - 博客频道 - CSDN.NET
boost:regex分割字符串(带有'\'字符) - zzusimon的专栏 - 博客频道 - CSDN.NET boost:regex分割字符串(带有'\'字符) 分类: C++ 2011-08- ...
- 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 ...
- profile对比std::regex与boost::regex的性能
c++11标准库的regex比boost库的regex之间的性能差距接近5倍,这是为什么?stackflow上也找到一篇post<c++11 regex slower than python&g ...
随机推荐
- 【leetcode】823. Binary Trees With Factors
题目如下: Given an array of unique integers, each integer is strictly greater than 1. We make a binary t ...
- js 运动框架-轻量级
具体代码如下: function move(obj,json,sv,fnEnd){ //CSS样式值 function getStyle(obj,attr){ if(obj.currentStyle) ...
- pgrep,pidof工具的使用
博客pgrep,pidof工具的使用 最灵活:ps 选项 | 其它命令 按预定义的模式:pgreppgrep [options] pattern-u uid: effective user,生效者-U ...
- 04 SecurityContextHolder与SecurityContext说明
该篇记录一下SecurityContextHolder与SecurityContext两个类,当然还有与它们关系密码的SecurityContextPersistenceFilter.java这个过滤 ...
- java 标准输入输出流,打印流,数据流
1 package stream; import static org.junit.Assert.assertNotNull; import java.io.BufferedReader; impor ...
- AGC033 D~F——[ 值放到角标的DP ][ 思路+DP ][ 思路 ]
地址:https://atcoder.jp/contests/agc033/ D Complexity dp[ i ][ j ][ k ][ l ] 表示左上角是 ( i , j ) .右下角是 ( ...
- 运行python不报错,运行pip报错
Fatal error in launcher: Unable to create process using '""c:\program files (x86)\python36 ...
- VB.NET导出Excel 轻松实现Excel的服务器与客户端交换 服务器不安装Office
说来VB.Net这个也是之前的一个项目中用到的.今天拿来总结下用途,项目需求,不让在服务器安装Office办公软件.这个也是煞费了一顿. 主要的思路就是 在导出的时候,利用DataTable做中间变量 ...
- 杂项-报表-Minitab:Minitab百科
ylbtech-杂项-报表-Minitab:Minitab百科 Minitab软件是现代质量管理统计的领先者,全球六西格玛实施的共同语言,以无可比拟的强大功能和简易的可视化操作深受广大质量学者和统计专 ...
- 用 Flask 来写个轻博客 (33) — 使用 Flask-RESTful 来构建 RESTful API 之二
Blog 项目源码:https://github.com/JmilkFan/JmilkFan-s-Blog 目录 目录 前文列表 扩展阅读 构建 RESTful Flask API 定义资源路由 格式 ...