Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QString split with QRegExp
Qt 6.11 is out! See what's new in the release blog

QString split with QRegExp

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 2.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cidadao
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • X Offline
      X Offline
      Xander84
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved