Extract parameters from string, included quoted regions, in Qt

前端 未结 3 1446
梦如初夏
梦如初夏 2021-01-22 07:27

I have a Qt5/C++ program which receives a QString containing a program name and possibly parameters. I need to split this QString into multiple strings. For example, the strin

3条回答
  •  感情败类
    2021-01-22 07:38

    Extract parameters from string, included quoted regions, in Qt

    When parsing any "escaped" or quoted string: splitArgs (from KF5) was immensely useful.

    The following source code manages "escaping" and more; from

        one two a\ b "c d e"
    

    it prints

        one
        two
        a b
        c d e
    

    The core of the source code is just a splitArgs call:

    #include 
    #include 
    
    int main()
    {
        QString whole = "one two a\\ b \"c d e\"";
        QTextStream(stdout) << whole << endl << "--------------------" << endl;
    
        QStringList group = KShell::splitArgs("one two a\\ b \"c d e\"");
        for (auto element: group) {
            QTextStream(stdout) << element << endl;
        }
    }
    

提交回复
热议问题