boost::regex

常见用法:

boost::regex express;

regex_match(str,express);  bool

smatch what;

cmatch what;

typedef match_results<const char*> cmatch;
typedef match_results<std::string::const_iterator> smatch;

regex_search(str,what,express);bool

what[0];

what[1];

regex_replace(str,express,kongge);

①匹配全串存在多个结果的情况下

主要针对string

boost::sregex_iterator ite(str.begin(), str.end(), express);
boost::sregex_iterator endite;
while (ite != endite)
{
cout<<*ite<<endl;
ite++;
}

char *p = "www.baidu.com---dffsfdsf--f-ff www.google.comddfdsffd";

boost::regex exp("\\w{3}\\.(\\w+\\.)+(net|com|cn)");

char *tmp = p;

cmath what;

while(boost::regex_search(tmp, what, exp))

{

  cout<<what[0]<<endl;

  tmp = what[0].second;

}

③可以通过const *p = s.c_str();

const_reference operator[](int n) const : 返回一个sub_match引用,当n=0时返回[first,last)的匹配结果,当0<n<=size时,返回对应元组的匹配结果,

当n>size时返回一个matched值为假的sub_match结构,元组按照左边括号的顺序和sub_match相对应,从1开始

一篇写得不错的博文

http://www.cnblogs.com/hucn/archive/2011/05/09/2041490.html

boost::regex iterator

http://www.boost.org/doc/libs/1_55_0/libs/regex/doc/html/boost_regex/ref/regex_iterator.html

时间: 2024-08-06 11:54:03

boost::regex的相关文章

boost::string or boost::regex

有时候写代码时会遇到下面问题 如果有一个文本文件,其包括内容类似于C语言,当中有一行例如以下格式的语句: layout (local_size_x = a,local_size_y = b, local_size_z = c) in; 当中用蓝色标记出的部分(layout, local_size_x, local_size_y, local_size_z, in)为keyword,斜体字部分(a, b, c)为数据类型为unsigned int的数字,请编写一个函数,用于从文件里抽取出a, b,

c++ 使用boost regex库 总结

用java的时候觉得挺折腾,回头来弄c++才知道什么叫折腾...汗... 首先参考我写的这篇文章:http://www.cnblogs.com/qrlozte/p/4100892.html 我从sourceforge把整个boost的zip下载下来以后,我主要是在编译 boost regex的时候出问题了:boost有很多library,regex只是其中一个,怎么编译regex? 当然,第一步是查看文档,找到boost-path/doc/html/index.html打开,翻了半天倒找rege

#墙裂推荐Boost regex# C,C++11,Boost三种regex库性能比较

在最近的一个项目中,发现之前的正则匹配模块对于长字符串匹配性能损失比较厉害,因此对长字符串下的各种正则匹配进行了略微研究并附有实例.本文参考了博客http://www.cnblogs.com/pmars/archive/2012/10/24/2736831.html(下文称文1),这篇文章也是对三种regex库进行了比较,但有些地方我还有一些自己的见解,特此罗列如下,感谢这篇文章的作者. 1.C regex库 由于项目中一直用的都是C regex库,所以首先对C regex进行了研究.对于C r

#include &lt;boost/regex.hpp&gt;

boost C++的正则表达式库boost.regex可以应用正则表达式于C++.正则表达式大大减轻了搜索特定模式字符串的负担,在很多语言中都是强大的功能. boost.regex库中两个最重要的类是boost::regex和boost::smatch,它们都在boost/regex.hpp文件中定义.前者用于定义一个正则表达式,而后者可以保存搜索结果. 小结:C++的正则表达式库早已有之,但始终没有哪个库纳入到标准化流程中.目前该库已经顺利的成立新一代C++标准库中的一员,结束了C++没有标准

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

在regex_search函数中,会将找到的第一个匹配结果保存到一个smatch类中. 然而如果搜索字符串中有多个匹配结果,则需要自己实现了. 在smatch中,有两个成员,官方文档如下: iterator first: An iterator denoting the position of the start of the match. iterator second An iterator denoting the position of the end of the match. 所以,

Boost::regex练习实例

#include <algorithm> #include <vector> #include <string> #include <iostream> #include <cassert> #include <boost/regex.hpp> using namespace std; using namespace boost; //写一个程序,提取<p></p>之间的内容 int main() { // r

Boost::Regex代码示例

看一个别人给出的例子,当我们输入“select 字符串 from 字符串”的时候,会得到输出:如果不符合这个格式,就会输出error.注意:在命令行下输入的时候,按ctrl+Z.回车表示行输入结束. #include <cstdlib> #include <stdlib.h> #include <boost/regex.hpp> #include <string> #include <iostream> using namespace std;

vs 2005 使用 boost regex

第一步: Boost 入门及其VS2005下编译boost库 boost.regex库安装指南 Boost下载和Boost安装去哪下载Boost呢?英文http://www.boost.org ,中文http://boost.c-view.org,可以找到一个.zip或.tar.gz格式的压缩包.下载完毕后,解压到某个目录,比如boost_1_26_0,里面一般有这么几个子目录:boost.libs.more.people.status.tools,看看没问题就行了.如果Boost更新时您懒得去

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(