簡體   English   中英

正則表達式 C++:提取標簽之間的子字符串

[英]Regex C++: extract substring between tags

我想在兩個標簽之間提取一些子字符串。 示例: <column r="1"><tb="red"><v>1</v></t></column>我想得到: <tb="red"><v>1</v></t>

我不想使用 boost 或其他庫。 只是來自 C++ 的標准東西,除了 CERN 的 ROOT 庫,帶有 TRegexp,但我不知道如何使用它......

應該使用正則表達式來嘗試匹配 html,但是,對於這種特殊情況,您可以這樣做:

#include <string>
#include <regex>

// Your string
std::string str = "<column r="1"><t b=\"red\"><v>1</v></t></column>";

// Your regex, in this specific scenario
// Will NOT work for nested <column> tags!
std::regex rgx("<column.*?>(.*?)</column>");
std::smatch match;

// Try to match it
if(std::regex_search(str.begin(), str.end(), match, rgx)) {
  // You can use `match' here to get your substring
};

正如安東所說: 不要

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM