提升分词器以将带引号的字符串视为一个标记
Boost tokenizer to treat quoted string as one token
有没有办法让 Boost 分词器在不拆分引用部分的情况下拆分字符串下方?
string s = "1st 2nd "3rd with some comment" 4th";
Exptected output:
1st
2nd
3rd with some comment
4th
您可以使用分词器库中的escaped_list_separator
。有关如何将其应用于您的问题的更多详细信息,请参阅此问题。
C++11 解决方案
#include <iostream>
#include <string>
#include <vector>
std::vector<std::string> tokenize(const std::string& str) {
std::vector<std::string> tokens;
std::string buffer;
std::string::const_iterator iter = str.cbegin();
bool in_string = false;
while (iter != str.cend()) {
char c = *iter;
if (c == '"') {
if (in_string) {
tokens.push_back(buffer);
buffer.clear();
}
in_string = !in_string;
} else if (c == ' ') {
if (in_string) {
buffer.push_back(c);
} else {
if (!buffer.empty()) {
tokens.push_back(buffer);
buffer.clear();
}
}
} else {
buffer.push_back(c);
}
++iter;
}
if (!buffer.empty()) {
tokens.push_back(buffer);
}
return tokens;
}
int main() {
std::string s = "1st 2nd "3rd with some comment" 4th";
std::vector<std::string> tokens = tokenize(s);
for (auto iter = tokens.cbegin(); iter != tokens.cend(); ++iter) {
std::cout << *iter << "n";
}
}
试试这段代码,这样你就可以避免使用Boost.Tokenizer和Boost.Spirit库
#include <vector>
#include <string>
#include <iostream>
const char Separators[] = { ' ', 9 };
bool Str_IsSeparator( const char Ch )
{
for ( size_t i = 0; i != sizeof( Separators ); i++ )
{
if ( Separators[i] == Ch ) { return true; }
}
return false;
}
void SplitLine( size_t FromToken, size_t ToToken, const std::string& Str, std::vector<std::string>& Components /*, bool ShouldTrimSpaces*/ )
{
size_t TokenNum = 0;
size_t Offset = FromToken - 1;
const char* CStr = Str.c_str();
const char* CStrj = Str.c_str();
while ( *CStr )
{
// bypass spaces & delimiting chars
while ( *CStr && Str_IsSeparator( *CStr ) ) { CStr++; }
if ( !*CStr ) { return; }
bool InsideQuotes = ( *CStr == '"' );
if ( InsideQuotes )
{
for ( CStrj = ++CStr; *CStrj && *CStrj != '"'; CStrj++ );
}
else
{
for ( CStrj = CStr; *CStrj && !Str_IsSeparator( *CStrj ); CStrj++ );
}
// extract token
if ( CStr != CStrj )
{
TokenNum++;
// store each token found
if ( TokenNum >= FromToken )
{
Components[ TokenNum-Offset ].assign( CStr, CStrj );
// if ( ShouldTrimSpaces ) { Str_TrimSpaces( &Components[ TokenNum-Offset ] ); }
// proceed to next token
if ( TokenNum >= ToToken ) { return; }
}
CStr = CStrj;
// exclude last " from token, handle EOL
if ( *CStr ) { CStr++; }
}
}
}
int main()
{
std::string test = "1st 2nd "3rd with some comment" 4th";
std::vector<std::string> Out;
Out.resize(5);
SplitLine(1, 4, test, Out);
for(size_t j = 0 ; j != Out.size() ; j++) { std::cout << Out[j] << std::endl; }
return 0;
}
它使用一个预分配的字符串数组(它不是从零开始的,但很容易修复),而且非常简单。
相关文章:
- 为什么 sscanf 无法从一个字符串中读取uint64_t和字符?
- 如何检查一个c++字符串中有多少相同的字符/数字
- 获取用C/C++打印的最后一个字符串
- cin >> int 给定一个字符串将 int 赋值为 0
- 我如何通过引用将 c++ 字符串分配给另一个字符串
- 使用另一个字符串从字符串中删除空格
- 有没有办法使用 strcpy 将字符串数组复制到另一个字符串或其他数组中?
- 当分配一个字符串值并稍后通过分配另一个值进行更改时C++如何管理内存?
- 我已经建立了递归关系,它找到了两个字符串之间最长的连续公共字符串,我怎么能跳过其中一个字符串中的一个字符
- C++为 void 指针分配一个字符串
- 如何在使用 getch 后在另一个字符串的末尾连接一个字符串
- 给定一个单词数组和一个字符串,如何计算给定字符串中的所有单词
- 如何在另一个字符串的x位置插入一个字符串?
- 有没有办法我可以在 C++ 中使用 if 语句来说明如果一个字符串不是两个名称的 eqal,它会给出一个输出,ATM i
- 在另一个字符串中插入文件扩展名之前的字符串
- 我有一个返回字符串向量的函数.它需要两个字符串,并且返回一个字符串中缺少的字符串
- C++自定义流操纵器,用于更改流上的下一个字符串
- 从类返回一个字符串 — 奇怪的行为
- 从我的2d数组中的一列返回的字符串值被压缩为一个字符串(在Mac os上打开Windows txt文件)
- 你能从"volatile const char*"构造一个字符串吗?(不使用`const_cast`