在VC6中使用正则表达式解析字符串

时间:2021-12-28 18:45:23

From: http://school.cnd8.com/vc/jiaocheng/33274.htm

参考上面网址中的文章,写了下面的例子,环境:windows XP SP3 +  VC6

#include <cstdlib >
#include <stdlib.h >
#include <boost/regex.hpp>
#include <string>
#include <iostream>
using namespace std;
using namespace boost;

int main(int argc, char * argv[])
{
regex expression("(\\d+)-(\\d+)-(\\d+)"); // 注意转义方式
string in("Today: 2007-06-23");
cmatch what;
// 如果用 regex_match 方法将需要完全匹配,
// 不能在字符串中找寻模式,可用于验证输入
if (regex_search(in.c_str(), what, expression))
{
for(int i = 0 ; i < what.size(); i++)
{
cout<<"str: "<<what[i].str()<<endl;
}
}
return 0;
程序运行结果:
str: 2007-06-23
str: 2007
str: 06
str: 23
Press any key to continue

 

注意:

本文所使用的boost库版本为:boost_1_34_0。请大家一定要看清楚了,一些高版本的boost库已经不再支持vc6了,而是支持更高版本的Visual Studio IDE(7.1以上)。

另外,编译之前,需要将libboost_regex-vc6-sgd-1_34.lib拷贝到程序根目录下,该文件是在“nmake -f vc6.mak”时产生的。