我想用boost匹配一个简单的表达式,但它的行为很奇怪......下面的代码应该匹配并显示第一个和第二个字符串中的“a”:
#include <iostream>
#include <boost/xpressive/xpressive.hpp>
#include "stdio.h"
using namespace boost::xpressive;
void xmatch_action( const char *line ) {
cregex g_re_var;
cmatch what;
g_re_var = cregex::compile( "\\s*var\\s+([\\w]+)\\s*=.*?" );
if (regex_match(line, what, g_re_var )) {
printf("OK\n");
printf(">%s<\n", what[1] );
}
else {
printf("NOK\n");
}
}
int main()
{
xmatch_action("var a = qqq");
xmatch_action(" var a = aaa");
xmatch_action(" var abc ");
}
但我的实际输出是:
OK
>a = qqq<
OK
>a = aaa<
NOK
它应该是
OK
>a<
OK
>a<
NOK