考虑引号之间的论点 [英] putting into consideration the argument between quotation marks
本文介绍了考虑引号之间的论点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试解析一个命令行字符串,在考虑每个空格时,该字符串在引号之间包含单词。我想将2个引号之间的任何内容存储为向量中的1个索引。
I am trying to parse a command line string, at every white space putting into consideration the string has words between quotation marks. I want to store whatever is between 2 quotation marks as 1 index in a vector.
vector<string> words;
stringstream ss(userInput);
string currentWord;
vector<string> startWith;
stringstream sw(userInput);
while (getline(sw, currentWord, ' '))
words.push_back(currentWord);
while (getline(ss, currentWord, '"'))
startWith.push_back(currentWord); //if(currentWord.compare("")){ continue;}
for (int i = 0; i < startWith.size(); i++)
curr
if(currentWord.compare("")){ continue;}
cout << " Index "<< i << ": " << startWith[i] << "\n";
推荐答案
目前尚不清楚您要做什么。这是一个起点(运行它):
It is not clear what you're trying to do. Here's a starting point (run it):
#include <iostream>
#include <sstream>
#include <string>
std::istream& get_word_or_quote( std::istream& is, std::string& s )
{
char c;
// skip ws and get the first character
if ( !std::ws( is ) || !is.get( c ) )
return is;
// if it is a word
if ( c != '"' )
{
is.putback( c );
return is >> s;
}
// if it is a quote (no escape sequence)
std::string q;
while ( is.get( c ) && c != '"' )
q += c;
if ( c != '"' )
throw "closing quote expected";
//
s = std::move( q );
return is;
}
int main()
{
std::istringstream is {"not-quoted \"quoted\" \"quoted with spaces\" \"no closing quote!" };
try
{
std::string word;
while ( get_word_or_quote( is, word ) )
std::cout << word << std::endl;
}
catch ( const char* e )
{
std::cout << "ERROR: " << e;
}
return 0;
}
预期输出为:
not-quoted
quoted
quoted with spaces
ERROR: closing quote expected
这篇关于考虑引号之间的论点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文