在上一篇boost学习文章《boost c++ library on linux初体验》中,主要讲了boost的非二进制库的使用,并实现了一个helloworld程序,此外还简单介绍了boost库的基本使用方法以及我在搭建自己的boost c++ on linux的编程环境过程中所遇到的问题和解决方案。
在本篇文章中,简要记录一下boost c++官网Getting started文档中的boost install以及在需要用到binary library时的demo程序。
/************************************************************************* > File Name: regex_example.cpp > Author: Liu Xin > Mail: [email protected] > Created Time: 2012年07月23日 星期一 21时08分28秒 > Description: > 提取邮件文本中的主题在console中输出 ************************************************************************/ #include<boost/regex.hpp> #include<iostream> #include<string> using namespace std; int main() { std::string line; boost::regex pat("^Subject:(Re:|Aw:)*(.*)"); while(std::cin){ std::getline(std::cin, line); boost::smatch matches; if(boost::regex_match(line, matches, pat)) { std::cout << "matches[0]: " << matches[0] << std::endl; std::cout << "matches[1]: " << matches[1] << std::endl; std::cout << "matches[2]: " << matches[2] << std::endl; }else{ std::cout << "mail text line: " << line << " doesn't match the subject pattern" << std::endl; } } return 0; }
$ c++ -I path/to/boost_1_50_0 example.cpp -o example ~/boost/stage/lib/libboost_regex-gcc34-mt-d-1_36.a
$ c++ -I path/to/boost_1_50_0 example.cpp -o example -L~/boost/stage/lib/ -lboost_regex-gcc34-mt-d-1_36
g++ -I /usr/local/boost_1_50_0/ regex_example.cpp -o regex_example /usr/local/boost_1_50_0/stage/lib/libboost_regex.a
To: George Shmidlap From: Rita Marlowe Subject: Will Sucess Spoil Rock Hunter? --- See subject.保存为mail.txt
./regex_example < mail.txt程序输出:
mail text line: To: George Shmidlap doesn't match the subject pattern mail text line: From: Rita Marlowe doesn't match the subject pattern matches[0]: Subject: Will Sucess Spoil Rock Hunter? matches[1]: matches[2]: Will Sucess Spoil Rock Hunter? mail text line: --- doesn't match the subject pattern mail text line: See subject. doesn't match the subject pattern mail text line: doesn't match the subject pattern
LDFLAGS=/usr/local/lib/libboost_regex.a CPPFLAGS=-I /usr/local/boost_1_50_0/ OBJS=regex_example.o PROGRAM=regex_example $(PROGRAM): regex_example.o g++ $(OBJS) -o $(PROGRAM) $(LDFLAGS) regex_example.o: regex_example.cpp g++ $(CPPFLAGS) -c regex_example.cpp -o $(OBJS) clean: rm *.o $(PROGRAM)