Chinaunix首页 | 论坛 | 博客
  • 博客访问: 356973
  • 博文数量: 78
  • 博客积分: 2222
  • 博客等级: 大尉
  • 技术积分: 745
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-24 10:48
文章分类

全部博文(78)

文章存档

2012年(7)

2011年(33)

2010年(38)

分类: C/C++

2010-11-01 09:50:25

1. Boost.Regex的编译
最新的Boost库是1.44.0,在使用Boost.Regex之前,需要先下载Boost库,可以通过下面两种方式下载:
(1)Http下载,下载地址是
(2)用svn客户端来下载,地址是:
我个人比较推荐使用svn客户端来下载,这样的话,如果要更新直接svn update一下,就不用再去重新下载了。

下载好了Boost库,下面开始编译, 本文中我以Linux平台为例进行说明,其它平台的编译方法参见上面提到的文档。下面是具体的步骤:(假设下载完后的,代码解压在了BOOST_ROOT目录)
(1)进入到BOOST_ROOT/libs/regex/build目录
(2)如果要使用静态库,请执行make -fgcc.mak
(3)如果要使用静态库,请执行make -fgcc-shared.mak
执行完上面三步后的,在BOOST_ROOT/libs/regex/build/下会生成一个gcc目录 ,进入该目录 ,可以看到生成了下面四个文件:
(1)libboost_regex-gcc-1_44.a , 这是release版的静态库
(2)libboost_regex-gcc-1_44.so, 这是release版的动态库(共享库)
(3)libboost_regex-gcc-d-1_44.a, 这是debug版的静态库
(4)libboost_regex-gcc-d-1_44.so, 这里debug版的动态库(共享库)
编译好之后的,就可以开始使用。

 2.Boost Regex Libray使用注意事项

(1)在使用之前你需要把Boost的安装目录加入到系统的Path中(当然也可以在编译时直接指定)
(2)需要包含的头文件 boost/regex.hpp
(3)需要依赖的库:上步中编译好的四个库,取其中任意一个即可,具体如何使用动态/静态库,请自己查阅相关资料

(4)程序编译成功后,在执行之前,还要在shell中运行:export LD_LIBRARY_PATH="path", path为你的libboost_regex-gcc-1_44.so所在的目录,然后再运行你的程序。

我的是:export LD_LIBRARY_PATH="/home/zhdr/test/boost/libs/regex/build/gcc"

3. Boost.Regex使用举例
下面是Boost.Regex使用的一个简单的例子:

#include <string>
#include <iostream>
 
#include "boost/regex.hpp"
 
int main(int argc, char ** argv)
{
    if (argc != 4)
    { 
        std::cerr < < "Usage: " << argv[0] << " option regex text\n"
            << "option: 0 --whole match\n"
            << " 1 --sub match\n"
            << " 2 --replace match\n";
        return 1;
    } 
 
    boost::regex oRegex(argv[2]);
    boost::smatch oResults;
    std::string strStr(argv[3]);
    std::string strRes;
 
    switch (atoi(argv[1]))
    { 
        case 0:
            if(boost::regex_match(strStr, oResults, oRegex))
            { 
                std::cout << strStr << " matches " << oRegex << "\n";
            } 
            else
            { 
                std::cout << strStr << " doesn't match " << oRegex << "\n";
            } 
            break;
        case 1:
            if(boost::regex_search(strStr, oResults, oRegex))
            {
                std::cout << strStr << " matches " << oRegex << "\n";
            }
            else
            {
                std::cout << strStr << " doesn't match " << oRegex << "\n"


完整参考:
阅读(3057) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-11-01 17:52:27

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com