QString split with QRegExp



  • Hi,

    Im trying to get the parameters of a C function by using QString split method with QRegExp.
    The string contains the text between the function parentheses so I need to extract the parameters that are separated by commas.
    I tried the following (tested with rubular.com):

    Regexp:
    @([^,]+(.+))|([^,]+)@

    Test string:
    @1, foo(1,2), 3@

    Which give me the correct result.
    Now, with Qt:

    @
    QString testStr = "1, foo(1,2), 3";
    QStringList arguments = testStr.split(QRegExp("([^,]+\(.+\))|([^,]+)"));
    qDebug() << "TEST STRING" << testStr << "ARGS" << arguments << "count =" << arguments.count();
    @

    And this is what I get:
    @TEST STRING "1, foo(1,2), 3" ARGS ("", ",", ",", "") count = 4@

    Am I missing something?



  • Hi, if you have access to Qt 5 you should try QRegularExpression instead of QRegExp, the new class QRegularExpression is more standard compliant to perl regex syntax and might fix your problem.
    I didn't check your regex yet, but if you say it works with other regex tools it might be worth a try.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.