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

C++11 正規表示式庫 實戰入門:搜尋、替換與迭代器完整指南

     編輯:關於C語言

C++11 正規表示式庫 <regex> 實戰入門:搜尋、替換與迭代器完整指南

C++11 標準庫引入 <regex>,提供強大、標準化的正規表示式支援,取代 Boost.Regex 等第三方庫。本文以實際 HTML 片段處理為例,示範搜尋、替換、多重匹配等核心用法。

一、核心類別與函式

類別/函式用途
std::regex編譯正規表示式模式
std::smatch匹配結果容器(string 專用)
std::regex_search搜尋第一個匹配
std::regex_replace替換所有匹配
std::sregex_iterator迭代所有匹配

二、實戰範例:HTML 圖片路徑重寫與標籤提取

測試資料

std::string html = 
    "<p><img src=\"D:\\DOC\\...\\main.png\" width=\"30%\" height=\"30%\"></p>"
    "<ol><li></li><li>中文歌</li><li></li><li>xxx</li><li></li></ol>"
    "<p><img src=\"D:\\DOC\\...\\main-about.png\" ...></p>"
    "<ol><li></li><li>xxxxx</li><li></li></ol>"
    "<p><img src=\"D:\\DOC\\...\\main-setting.png\" ...></p>";

1. 替換:將絕對路徑轉為相對路徑

// 模式:捕獲群組 1=
std::regex img_regex(R"((]*src=["'])[^"']*\\(images\\[^"']*["'][^>]*>))");
std::string rep = "$1$3";  // 保留群組 1 與 3,丟棄中間絕對路徑
std::string result = std::regex_replace(html, img_regex, rep);

輸出:所有 src 變為 images\main\main.png 等相對路徑。

2. 搜尋第一個 <img> 標籤

std::regex tag_regex(R"(]*>)");
std::smatch match;
if (std::regex_search(html, match, tag_regex))
    std::cout << match[0] << '\n';  // 輸出第一個完整 img 標籤

3. 迭代所有 <img> 標籤

auto begin = std::sregex_iterator(html.begin(), html.end(), tag_regex);
auto end = std::sregex_iterator();
for (auto it = begin; it != end; ++it) {
    std::cout << it->str() << '\n';  // 依序輸出 3 個 img 標籤
}

三、完整可編譯程式碼

#include <regex>
#include <string>
#include <iostream>

int main() {
    std::string html = 
        "<p><img src=\"D:\\\\DOC\\\\...\\\\main.png\" width=\"30%\" height=\"30%\"></p>"
        "<ol><li></li><li>中文歌</li><li></li><li>xxx</li><li></li></ol>"
        "<p><img src=\"D:\\\\DOC\\\\...\\\\main-about.png\" width=\"30%\" height=\"30%\"></p>"
        "<ol><li></li><li>xxxxx</li><li></li></ol>"
        "<p><img src=\"D:\\\\DOC\\\\...\\\\main-setting.png\" width=\"30%\" height=\"30%\"></p>";

    // 替換展示
    std::regex img_regex(R"((]*src=["'])[^"']*\\(images\\[^"']*["'][^>]*>))");
    std::cout << "=== Replace ===\n" << std::regex_replace(html, img_regex, "$1$3") << "\n\n";

    // 搜尋第一個
    std::regex tag_regex(R"(]*>)");
    std::smatch match;
    std::cout << "=== First match ===\n";
    if (std::regex_search(html, match, tag_regex))
        std::cout << match[0] << "\n\n";

    // 迭代所有
    std::cout << "=== All matches ===\n";
    for (auto it = std::sregex_iterator(html.begin(), html.end(), tag_regex);
         it != std::sregex_iterator(); ++it)
        std::cout << it->str() << "\n";
    return 0;
}

四、關鍵注意事項

  1. 原始字串字面值 R"(...)" 避免雙重跳脫,提升可讀性
  2. 模式錯誤會拋出例外 std::regex_error,建議包裝 try-catch
  3. std::regex 建構較昂貴,重複使用時應複用物件
  4. 預設語法為 ECMAScript,可指定 std::regex::extended 等標準
  5. 大量資料處理時,regex_replace 會複製整個字串,考慮串流處理或手寫解析器

五、常用模式速查

模式說明
\d數字
\w字母數字底線
\s空白字元
.任意字元(除換行)
* + ?0或多 / 1或多 / 0或1
^ $行首 / 行尾
(...)捕獲群組
(?:...)非捕獲群組
|或

<regex> 讓 C++ 終於擁有標準化、型別安全的正規表示式能力。掌握 regex_search、regex_replace、sregex_iterator 三大核心工具,即可應對 90% 字串處理需求,告別手寫解析器的痛苦。

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