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

4

2 回答 2

1

AFAIK,没有这样的选择。请参阅了解标记的子表达式和捕获,特别是关于 Perl 和 Boost.Regex 等价的表。您将需要使用boost::match_results<IteratorType>访问任何和所有匹配项。

于 2010-03-12T12:56:12.103 回答
1

我终于找到了我想要达到的目标。

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

我试过了,它会毫无问题地工作。

我认为这是自 boost 1.42 版本以来才有的功能,但我不确定。

于 2010-03-12T16:11:16.947 回答