推荐正则表达式入门教程《正则表达式30分钟入门教程》,作者为deerchao。
以boost1.42为例
1.编译boost regex库
Boost的大部分库不需要编译,regex库是为数不多的需要编译的boost子库之一。
找到libs目录,进入regex的build目录。
打开命令行,进入这个目录。输入以下命令:
nmake vc9.mak
会生成一个vc90目录。存放l编译好的ib和dll文件。我使用vs2008,所以选择了vc9.mak
2.调试第一个boost regex例子
源码如下
#include <iostream>
#include <boost/regex.hpp>
using namespace std;
int main(int argc, char* argv[])
{
const char * szStr="your telephone number is 010-65897489";
boost::regex reg( "\\d\\d\\d-\\d\\d\\d\\d\\d\\d\\d\\d"); //查找字符串里的数字
boost::cmatch mat;
if(boost::regex_search(szStr, mat, reg))
{
cout << "searched:" << mat[0] << endl;
}
cin.get();
return 0;
}
编译时会出现
LINK : fatal error LNK1104: 无法打开文件“libboost_regex-vc90-mt-gd-1_42.lib” 错误。将编译好的lib文件,添入附加目录就好了。
再次编译
运行结果如下
searched:010-65897489

本文介绍如何编译 Boost 的 regex 库并提供了一个简单的示例程序。通过此教程,读者可以了解 Boost regex 库的基本使用方法,包括正则表达式的定义、匹配及搜索等操作。

848

被折叠的 条评论
为什么被折叠?



