描述文本模式的強大工具。Boost.Regex 提供標準 C++ 介面。
#include <boost/regex.hpp>
#include <string> <iostream>
using namespace std; using namespace boost;
regex expression("^select ([a-zA-Z]*) from ([a-zA-Z]*)");
int main() {
string in; cmatch what;
cout << "enter test string" << endl;
getline(cin, in);
if (regex_match(in.c_str(), what, expression)) {
for (int i=0; i<what.size(); ++i)
cout << "str: " << what[i].str() << endl;
} else cout << "Error Input" << endl;
return 0;
}
輸入 select name from table
輸出 整句 / name / table 三組匹配結果。
需單獨編譯 regex 庫。
cmd
vcvars32.bat
cd D:\boost_1_32_0\libs\regex\build
d:
nmake -fvc6.mak
nmake -fvc6.mak install
解壓後 ~100MB,編譯後 ~131MB,需預留空間。
chdir E:\Program\boost_1_34_1
bjam "-sTOOLS=vc-7_1" "-sVC71_ROOT=D:\Program Files\Microsoft Visual Studio .NET 2003\Vc7"
"--prefix=E:\Program\boost" "--builddir=E:\Program\boost_1_34_1\build"
"-sBUILD=debug release <runtime-link>static/dynamic" --with-regex install
PAUSE
參數詳見 Boost 官方文檔。
VC6: Tools → Options → Directories → Include files 加入 Boost 根目錄。
VS2003: 專案屬性 → C/C++ → Additional Include Directories。