2010-03-12 96 views
3

我正在使用新版本的boost 1.42,我想使用正則表達式和命名子組。下面是一個例子。獲取名稱子組

std::string line("match this here FIELD=VALUE in the middle"); 
boost::regex rgx("FIELD=(?<VAL>\\w+)", boost::regex::perl); 
boost::smatch thisMatch; 
boost::regex_search(line, thisMatch, rgx); 

你知道如何獲取比賽內容嗎? 傳統的方式是

std::string result(mtch[1].first, mtch[1].second); 

我不想用這種方式。

我想像往常一樣在Perl和正則表達式中使用子組的名稱。 我試過了,但沒有奏效。

std::string result(mtch["VAL"].first, mtch["VAL"].second); 

您知道如何使用子組的名稱獲取值嗎?

感謝 AFG

+0

你想要的方式和你不想要的方式有什麼區別?這些陳述完全相同。 – kennytm 2010-03-12 12:59:21

+0

你說得對。我剛剛更改了示例中的代碼 – 2010-03-12 15:59:24

+0

再次!看看下面'因爲我找到了我需要的東西! 週末愉快! – 2010-03-12 16:21:06

回答

1

據我所知,沒有這樣的選項。請參閱Understanding Marked Sub-Expressions and Captures,特別是Perl和Boost.Regex等價的表。您需要使用boost::match_results<IteratorType>才能訪問任何和所有匹配項。

+0

嗨,dirkgently!我感謝你的網址。我找到了解決方案,請參閱下面我寫的內容! 週末愉快! – 2010-03-12 16:20:06

1

我終於找到了自己想達到的目標。

std::cout << mtch["VAL"] << std::endl; 

我試過了,它會工作沒有任何問題。

我認爲這是一個功能只有從版本1.42升壓,但我不知道。