たまにはg++のリハビリをって事で、放置していた正規表現に手を出してみる。
さて、適当なDebian systemにboostを入れてみた。後でsqueezeと気づいたけどwheezyでも大差ないよね、うん。
コンパイルオプションを忘れないでね。
PRG=dirlist CC=g++ EDITOR=vim all: $(CC) -o $(PRG) -lboost_regex $(PRG).cpp run: ./$(PRG) editor: $(EDITOR) $(PRG).cpp clean: rm -f $(PRG) *~
ふむ。regexでなくxpressiveを使ってみた。こっちが新しいんだよね確か(確信なし)。
なんか余計なものを include しているが気にするな。
#include <fstream> #include <iostream> #include <string> #include <sys/types.h> #include <dirent.h> #include <unistd.h> #include <boost/xpressive/xpressive.hpp> using namespace std; int main() { using namespace boost::xpressive; //sregex reg = sregex::compile("html"); //sregex reg = sregex::compile("\\.html$"); sregex reg = sregex::compile("([Rr]adio|[Nn]ovel)"); // boost::regex reg("*\.html$"); //boost::regex reg("(Radio|novel)"); // boost::regex reg = "html"; smatch m; string x; DIR* dp=opendir("../WebWiki/"); if (dp!=NULL) { struct dirent* dent; do{ dent = readdir(dp); if (dent!=NULL) { x = dent->d_name; if(regex_search(x, reg)) { cout<<dent->d_name<<endl; } } }while(dent!=NULL); closedir(dp); } }
あとは、 make して make run すれば走る。
$ make clean;make;make run rm -f dirlist *~ g++ -o dirlist -lboost_regex dirlist.cpp ./dirlist novelCs.html novelJava.html novelD1.html Novell.html RadioLAN.html $