![](/img/trans.png)
[英]boost::regex, match_results::operator[] - Mysterious “sub += 2” line
[英]Getting sub-match_results with boost::regex
嘿,讓我說我有這個正則表達式:( (test[0-9])+
而且我將它與: test1test2test3test0
匹配
const bool ret = boost::regex_search(input, what, r);
for (size_t i = 0; i < what.size(); ++i)
cout << i << ':' << string(what[i]) << "\n";
現在, what[1]
將是test0
(最后一次出現)。 讓我們說我需要得到test1
和3:我該怎么辦?
注意:真正的正則表達式非常復雜,並且必須保持一個整體匹配,因此將示例正則表達式更改為(test[0-9])
將不起作用。
我認為Dot Net能夠制作單個捕獲組集合,以便(grp)+將在group1上創建一個集合對象。 boost引擎的regex_search()就像任何普通的匹配函數一樣。 你坐在while()循環中,匹配最后一個匹配的模式。 您使用的表單不使用bid-itterator,因此該函數不會啟動最后一個匹配停止的下一個匹配項。
您可以使用itterator表單:
( 編輯 - 您還可以使用令牌迭代器,定義要迭代的組。在下面的代碼中添加)。
#include <boost/regex.hpp>
#include <string>
#include <iostream>
using namespace std;
using namespace boost;
int main()
{
string input = "test1 ,, test2,, test3,, test0,,";
boost::regex r("(test[0-9])(?:$|[ ,]+)");
boost::smatch what;
std::string::const_iterator start = input.begin();
std::string::const_iterator end = input.end();
while (boost::regex_search(start, end, what, r))
{
string stest(what[1].first, what[1].second);
cout << stest << endl;
// Update the beginning of the range to the character
// following the whole match
start = what[0].second;
}
// Alternate method using token iterator
const int subs[] = {1}; // we just want to see group 1
boost::sregex_token_iterator i(input.begin(), input.end(), r, subs);
boost::sregex_token_iterator j;
while(i != j)
{
cout << *i++ << endl;
}
return 0;
}
輸出:
test1
test2
test3
test0
Boost.Regex為這個功能提供了實驗支持(稱為重復捕獲); 但是,由於它的性能很高,默認情況下會禁用此功能。
要啟用重復捕獲,您需要重建Boost.Regex並在所有轉換單元中定義宏BOOST_REGEX_MATCH_EXTRA
; 最好的方法是在boost / regex / user.hpp中取消注釋這個定義(參見參考資料 ,它位於頁面的最底部)。
使用此定義編譯后,您可以通過使用match_extra
標志調用/使用regex_search
, regex_match
和regex_iterator
來使用此功能。
有關詳細信息,請查看Boost.Regex的參考。
在我看來,你需要創建一個regex_iterator ,使用(test[0-9])
正則表達式作為輸入。 然后,您可以使用生成的regex_iterator
枚舉原始目標的匹配子字符串。
如果你仍然需要“一個整體匹配”,那么也許這項工作必須與尋找匹配子串的任務分離。 你能澄清一下你的要求嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.