提升精神提取第一个单词并将其存储在向量中
我在 Boost.Spirit 解析字符串时遇到问题。
该字符串看起来像
name1 has this and that.\n
name 2 has this and that.\n
na me has this and that.\n
,我必须提取名称。 文本“有这个和那个”总是相同的,但名称可以包含空格,因此我不能使用 graph_p。
1)如何解析这样的字符串?
由于该字符串有几行该格式,因此我必须将名称存储在向量中。
我使用类似的方法
std::string name;
rule<> r = *graph_p[append(name)];
来保存一个名称,但是
2)在向量中保存多个名称的最佳方法是什么?
预先感谢
康拉德
I have problems with Boost.Spirit parsing a string.
The string looks like
name1 has this and that.\n
name 2 has this and that.\n
na me has this and that.\n
and I have to extract the names. The text "has this and that" is always the same but the name can consist of spaces therefore I can't use graph_p.
1) How do I parse such a string?
Since the string has several lines of that format I have to store the names in a vector.
I used something like
std::string name;
rule<> r = *graph_p[append(name)];
for saving one name but
2) what's the best way to save several names in a vector?
Thanks in advance
Konrad
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为这可以解决问题:
I think this will do the trick:
如果您使用较新的 Spirit V2.x(这是自 Boost V1.42 以来的默认设置),这很简单:
之后,如果
result
为true
,则vectornames
将保存所有已解析的名称(使用 Boost V1.45 进行测试)。If you use the newer Spirit V2.x (which is the default since Boost V1.42), this is as easy as:
After which, if
result
istrue
, the vectornames
will hold all parsed names (tested with Boost V1.45).我认为您使用 Boost 是有原因的。 Spirit 而不是 STL 的 string 的 查找方法? 例如:
I presume there is a reason why you are using Boost.Spirit and not STL's string's find method? E.g:
要删除“有这个和那个”,请使用:
To remove the "has this and that" use: