如何在使用boost.xpressive的sregex_token_iterator循环中判断匹配的规则?
我想对一个字符串进行字段分析,比如“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
)匹配成功,然后才能做相应操作。
pptp1
10 years, 5 months ago
Answers
简单说你用 semantic action 就可以,比如:
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*/ ]
;
【哔——】
answered 10 years, 5 months ago