boost_1_33_1.zip)D:\boost (路徑無空格、中文)> cmd
> cd D:\boost\libs\regex\build
> "D:\Program Files\Microsoft Visual Studio\VC98\Bin\VCVARS32.BAT" // 設定 VC6 環境變數
> nmake -fvc6.mak
// 產生 vc6\ 目錄,含 .lib / .dll
雙擊執行無效 (環境變數隨視窗關閉消失)。正確做法:cmd → 拖曳 VCVARS32.BAT 到視窗 → Enter → 再執行 nmake。
D:\boost\libs\regex\build\vc6\*.lib 複製至專案目錄或統一庫目錄 (如 C:\Program Files\Microsoft Visual Studio\VC98\BoostLib)D:\boostC:\Program Files\Microsoft Visual Studio\VC98\BoostLib#include
#include
#include
using namespace std;
using namespace boost;
int main() {
regex expr("^select ([a-zA-Z]*) from ([a-zA-Z]*)");
string input;
cmatch what;
cout << "Enter SQL: ";
getline(cin, input);
if (regex_match(input.c_str(), what, expr)) {
for (int i = 0; i < what.size(); ++i)
cout << "Group " << i << ": " << what[i] << endl;
} else {
cout << "Not match" << endl;
}
return 0;
}
void CMyDlg::OnBtnTestRegex() {
boost::regex expr("^select ([a-zA-Z]*) from ([a-zA-Z]*)");
CString strInput = _T("select name from users");
boost::cmatch what;
// CString → const char* (ANSI 建置)
if (boost::regex_match(static_cast(strInput), what, expr)) {
for (int i = 0; i < what.size(); ++i) {
CString strMatch(what[i].str().c_str()); // std::string → CString
AfxMessageBox(strMatch);
}
} else {
AfxMessageBox(_T("Not match"));
}
}
CString 為 wchar_t*,需用 boost::wregex、boost::wcmatchCStringA strA(strInput); (MFC 7.0+)、CT2CA (ATL 轉換巨集)| 錯誤 | 原因 | 解決 |
|---|---|---|
| 找不到 boost/regex.hpp | Include 目錄未加入 boost 根目錄 | Tools→Options→Directories→Include files 加入 D:\boost |
| LNK2001: 未解析的外部符號 _regex_match | 未連結 regex 庫 | Library files 加入庫目錄,專案設定→Link→Object/Library modules 加入 boost_regex.lib |
| fatal error C1083: 無法開啟編譯的類型庫 | 預編譯標頭衝突 | stdafx.h 先包含 boost/regex.hpp,或關閉預編譯標頭 |
| nmake 非內部命令 | 未執行 VCVARS32.BAT | 同一 cmd 視窗先執行 VCVARS32.BAT |
libs/regex/build,節省大量時間 (完整編譯需 1-2 小時)boost_regex.lib) 部署簡單;動態連結 (boost_regex.dll) 需隨程式部署 DLL完成上述步驟,您的 VC6.0 專案即擁有現代化、標準化的正則表達式能力,輕鬆應對複雜字串解析、驗證、提取需求。