现在的位置: 首页 > 综合 > 正文

boost正则表达式入门

2018年02月15日 ⁄ 综合 ⁄ 共 703字 ⁄ 字号 评论关闭

推荐正则表达式入门教程《正则表达式30分钟入门教程》,作者为deerchao

boost1.42为例

1.编译boost regex

  Boost的大部分库不需要编译,regex库是为数不多的需要编译的boost子库之一。

找到libs目录,进入regexbuild目录。

      打开命令行,进入这个目录。输入以下命令:

      nmake vc9.mak

     会生成一个vc90目录。存放l编译好的ibdll文件。我使用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

抱歉!评论已关闭.