簡體   English   中英

在c ++正則表達式上需要幫助

[英]need help on c++ Regex

我嘗試使以下代碼適用於我的gcc 4.8.1,但我做不到。 如果您需要更多信息,請告訴我。 非常感謝

std::cmatch res;
std::string str = "<h2>I'm a piece of text</h2>";
std::regex rx("<h(.)>([^<]+)");
std::regex_search(str.c_str(), res, rx);
std::cout << res[1] << ". " << res[2] << "\n";

輸出:

2. Egg prices

根據克里斯,我需要等待海灣合作委員會4.9。 如果那樣,您如何在當前gcc(而不是boost)中實現此設計? 我想檢索比賽

再次感謝。

根據錯誤消息,您必須使用另一個庫,例如Boost.Regex

如果要清理HTML,則應考慮使用更專業的技術。 我個人通過jsoup做到這一點。 Gumbo可能適用於C ++。 XML解析器通常也可以使用。

我現在沒有增強環境來測試(我會盡快進行),但是如果您有,請嘗試以下方法:

#include<boost/regex>
#include<iostream>
#include<string>

int main(){

try{
  std::string str("<h2>I'm a piece of text</h2>");

  boost::regex rx("(<h[1-9]>)(.*)(<\\/h[1-9]>)");
  boost::sregex_iterator it(str.begin(), str.end(), rx);

  std::cout << << (*it)[1] << "\n"; // get group 1

 return 0;
}

正則表達式101是驗證您的正則表達式的絕佳來源!

暫無
暫無

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

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