2015-06-28 68 views
-5

我想提取兩個標籤之間的某個子串。 例子:<column r="1"><t b="red"><v>1</v></t></column> 我想獲得:<t b="red"><v>1</v></t>正則表達式C++:提取標籤之間的子串

我不想使用升壓或其他庫。從C++,只是標準的東西,除了CERN的ROOT lib下,與TRegexp,但我不知道如何使用它...

+0

你能舉一個你想要的具體例子嗎? – paulotorrens

+6

http://stackoverflow.com/a/1732454/3959454 –

回答

2

應該使用正則表達式試圖匹配HTML,但是,這特殊情況下,你可以這樣做:don't

#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 
}; 

安東上面說的。

+1

謝謝。是工作 –

相關問題