首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> VC >> VC 教學

C++ Boost.Regex 正則表達式庫使用指南:安裝、編譯與實戰範例

     編輯:VC 教學

什麼是正則表達式

描述文本模式的強大工具。Boost.Regex 提供標準 C++ 介面。

經典範例:SQL-like 查詢解析

#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 三組匹配結果。

Boost.Regex 編譯安裝

需單獨編譯 regex 庫。

VC6 命令列

cmd
vcvars32.bat
cd D:\boost_1_32_0\libs\regex\build
d:
nmake -fvc6.mak
nmake -fvc6.mak install

解壓後 ~100MB,編譯後 ~131MB,需預留空間。

VS2003 批次檔

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 官方文檔。

IDE 整合

VC6: Tools → Options → Directories → Include files 加入 Boost 根目錄。
VS2003: 專案屬性 → C/C++ → Additional Include Directories。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved