我想对一个字符串进行字段分析,比如“a+b:c"。我使用了boost.xpressive
,程序片段如下(忽略了库的包含和命名空间的使用):
sregex reOp = as_xpr('+') | '-' | '*' | '/';
sregex reDelim = as_xpr(':');
sregex reField = +_w;
sregex reSimpleToken = reOp | reDelim | reField;
string str = "a+b:c";
sregex_token_iterator cur(str.begin(), str.end(), reSimpleToken), end;
for (; cur != end; ++cur) {
cout << *cur << endl;
}
会打印出:
a
+
b
:
c
但是我希望能够在循环里获知是哪一个规则(reOp
,reDelim
和reField
)匹配成功,然后才能做相应操作。
PHP中文网2017-04-17 11:27:27
To put it simply, you can use semantic action, such as:
sregex reOp = as_xpr('+') | '-' | '*' | '/';
sregex reDelim = as_xpr(':');
sregex reField = +_w;
sregex reSimpleToken = (s1=reOp)[ /*Do something about reOp here*/ ]
| (s1=reDelim)[ /*Do something about reDelim here*/ ]
| (s1=reField)[ /*Do something about reField here*/ ]
;