全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1803
推到 Plurk!
推到 Facebook!

請問如何找到boost::regex 的所有match和submatch?

尚未結案
holyguard
一般會員


發表:4
回覆:3
積分:1
註冊:2008-12-09

發送簡訊給我
#1 引用回覆 回覆 發表時間:2009-02-23 03:19:52 IP:122.117.xxx.xxx 訂閱

[code cpp]
std::string mystring = "(\\d\\d)(\\w\\w)";
boost::regex e(mystring);
std::string mystring2 = "11ww 22tt 66dd";
boost::smatch match;
if(boost::regex_search(mystring2, match, e))
{
Memo1->Lines->Add(match.size());
Memo1->Lines->Add(match.length());
for(unsigned int i=0; i {
Memo1->Lines->Add(match[i].str().c_str());
}
}
[/code]


得到match.size()=3,match.length()=4, match的結果為11ww 11 ww共三個

我的問題是:照理來講應該要能夠成功找到11ww 22tt 66dd三個match,各有兩個submatch: 11 ww, 22 tt, 66 dd
請問一下我程式要怎麼寫才能將所有的match和submatch找到呢?()括號裏面的東西,除了叫submatch,也有人稱為capture groups
目前上網找,只有找到vc#2008的範例,用match.[i].Groups.[j].Value,跑完i和j的for loop,就能將所有match的結果列出來,因為vc#2008將submatch放在Groups集合裏面。還沒找到c 的範例一次可以找出多個match和submatch的
這問題卡了我10天了,如蒙回答 不勝感激
holyguard
一般會員


發表:4
回覆:3
積分:1
註冊:2008-12-09

發送簡訊給我
#2 引用回覆 回覆 發表時間:2009-02-23 04:51:49 IP:122.117.xxx.xxx 訂閱
剛剛在boost網站上找到範例(k了5遍以上才發覺這個範例…唉…英文該加強了)
果然boost::smatch也有我預期中的captures這個member,要用的話就是在regex_search加上boost::match_extra這個參數即可
範例可參考:
http://www.boost.org/doc/libs/1_35_0/libs/regex/doc/html/boost_regex/captures.html
但是我compile的時候過不了,系統提示:'captures' is not a member of 'boost::smatch'
請問要怎麼解決呢?
我用的是bcb2009和boost1.35
include boost的方法是參考codegear的網站:
http://blogs.embarcadero.com/davidi/2008/07/21/38911
holyguard
一般會員


發表:4
回覆:3
積分:1
註冊:2008-12-09

發送簡訊給我
#3 引用回覆 回覆 發表時間:2009-02-23 06:30:36 IP:122.117.xxx.xxx 訂閱
不好意思快變成自問自答了…

我來回答一下:
使用boost::regex,要找到一個以上的captures,要做兩件事
1、找boost/regex/user.hpp,找到這一行://#define BOOST_REGEX_MATCH_EXTRA,把前面的「//」註解符號拿掉
2、加上boost::match_extra這個參數
例如:
if(boost::regex_search(mystring2, match, e, boost::match_extra))
yourcode here...
系統時間:2024-03-29 14:32:56
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!