c++ - 使用 boost::tokenizer 标记包装的字符串

标签 c++ boost tokenize

我正在解析一个自定义元数据文件,其中分隔符是空格 ' '。元数据文件包含应省略空格分隔符的字符串。所以 "\"This Space\"" 是一个标记,"This Space" 应该是两个标记。

有这样的问题here以及如何在不使用 boost::tokenizer 的情况下获得结果的答案。这似乎是标记器的默认任务,我认为这应该可以使用 boost::tokenizer

我写了一个例子来展示我到目前为止做了什么:

#include <boost/tokenizer.hpp>
#include <vector>
#include <string>
#include <iostream>

using std::string;
using data = std::vector<string>;

data buildExpected()
{
    string s[] = {"This", "is one of", "42", "lines" };
    return data(s, s + 4);
}

data tokenizeLine(string line)
{
    using namespace boost;
    data d;
    char_separator<char> sep("\" ");
    tokenizer<char_separator<char>> tokens(line, sep);
    for (string tok : tokens) d.push_back(tok);
    return d;
}

void logData(string id, data &d)
{
    string line = "(" + id + "):";
    bool more = 0;
    for (auto s : d)
    {
        if (more) line += "; ";
        more = 1;
        line += s;
    }
    std::cout << line << std::endl;
}

void main()
{
    string line = "This \"is one of\" 42 lines";
    data expected = buildExpected();
    data actual = tokenizeLine(line);
    logData("expected", expected);
    logData("actual  ", actual);

}

这是我系统的输出:

sample output

最佳答案

Boost.Tokenizer 不处理引号。它的功能非常简单 - 只需在每个分隔符出现时拆分为标记。您需要自己处理括号。

关于c++ - 使用 boost::tokenizer 标记包装的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34177549/

相关文章:

c++ - 基于 bool 模板参数的启用方法

C++:使用两种内在类型的运算符作为函数对象

c++ - 使用 fstream 设置精度以输出文件 - double 格式

python - 如何避免 NLTK 的句子分词器在缩写时 split ?

python - 如何使用 Python nltk.tokenize 将包含停用词的短语视为单个标记

c++ - Makefile 不评估 OBJECTS 变量

c++ - std::pair 提示类型不完整

c++ - 如何在 C++ 中使用 boost::regex 获取字符串?

c++ - Boost 库,如何从 lu_factorize() 获取行列式?

c++ - 使用 find 和 substr 函数进行解析