获取与升压::正则表达式的最后一场比赛 [英] Get last match with Boost::Regex
问题描述
我在C ++与升压常规的前pression这匹配多行字符串行。正则表达式搜索找到的第一场比赛,但我感兴趣的是相匹配的最后一行。
I have a regular expression in C++ with Boost which matches lines in multi-line strings. Regex search finds the first match, however I'm interested in the last line which matches.
在code,我现在使用的是这样的:
The code I'm using now is something like this:
matched = boost::regex_search(input, results, regex);
if (!matched) {
return -1; // error code
}
matched_string = results["Group"];
如果正则表达式
是(小于?组>数据)
和输入
是数据1数据2数据3
,那么 matched_string
现在数据1
。我想这是数据3
。
If regex
was "(?<Group>Data.)"
and input
was "Data1 Data2 Data3"
, then matched_string
is now "Data1"
. I want it to be "Data3"
.
推荐答案
运算符[]
的match_results
的返回 sub_match
。 sub_match
继承的std ::对
的迭代器。
它的第一
和第二
成员对应匹配的范围。
所以,你可以使用它的第二
新搜索的起点。
例如:
operator[]
of match_results
returns a sub_match
.
sub_match
inherits std::pair
of iterators.
Its first
and second
members correspond to matched range.
So, you can use its second
for start point of new search.
For example:
string input = "Data1 Data2 Data3";
regex re("(?<Group>Data.)");
string::const_iterator begin = input.begin(), end = input.end();
smatch results;
while ( regex_search( begin, end, results, re ) ) {
smatch::value_type r = results["Group"];
begin = r.second;
}
希望这有助于。
这篇关于获取与升压::正则表达式的最后一场比赛的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!