Important: Please read the Qt Code of Conduct -

QString.split() Which QRegExp to split parameters?

  • Hi,
    I have a small problem with the split function. I receive a text line from a bluetooth modul which includes the bluetooth ID and the name (NAME 00:12:34:56:78:90 "My Name"). My function should return me the single parameters. Currently I use the following code to separate the string:

    QString stringCmd, stringId, stringName;
    QString line = "NAME 00:12:34:56:78:90 ""My Name""";	//NAME 00:12:34:56:78:90 "My Name"
    QStringList parameter;
    parameter = line.split(QRegExp("\\s+"));
    stringCmd =;	//stringCmd == NAME
    stringId =;	//stringId == 00:12:34:56:78:90
    stringName =;	//stringName == My	--> should be "My Name"
    qDebug() << "Parameter" << stringCmd << stringId << stringName;
    //Output of qDebug(): Parameter "NAME" "00:12:34:56:78:90" "My"

    I tried to separate the string by spaces, but my problem is that the string includes a string with spaces. Parameter 2 should include the complete name inside the double quotes with spaces ("My Name"). Can anyone give me a hint which QRegExp to use?

  • Moderators

    is this format always the same?
    Because then it would be easier to use reg-exp capturing instead:

    QRegExp rx("([a-z]+)\\s+([0-9]{2}:[0-9]{2}:[0-9]{2}:[0-9]{2}:[0-9]{2}:[0-9]{2})\\s+\"([a-z0-9\\s]+)\"", Qt::CaseInsensitive, QRegExp::RegExp2);
    if( rx.indexIn( line ) >= 0 )
           stringCmd = rx.cap(1);
           stringId = rx.cap(2);
           stringName = rx.cap(3);

  • Thanks for your fast answer. The format is not every time the same, also the number of the parameter can vary. I tried your code and it works for this fixed format. I'm looking for a more general solution. Any ideas?

  • Lifetime Qt Champion


    What exactly can vary ? Can you provide some samples ?

    Also, you should rather use QRegularExpression, QRegExp has been deprecated in Qt 5.

    Theres' the Regular Expression Example that you can build to help you write the regex you need.

  • Moderators

    @_M_H_ said in QString.split() Which QRegExp to split parameters?:

    I tried your code and it works for this fixed format. I'm looking for a more general solution. Any ideas?

    depends how the data you receive looks like.
    If the first command parameter determines the message type you can do something like this:

    const QString command = receivedLine.mid( 0, receivedLine.indexOf(QRegularExpression("\\s") ) );
    if( command == QLatin1String("NAME") )
          // do specific parsing
    else if( command == QLatin1String("...") )
         // do specific parsing

  • Thanks for your answers. If I understand it right it is not possible with as single line command. I have to do further analysis of the incoming strings. I wanted to create a helper function which returns the command and parameters from a bluetooth modul. For pairing a headphone it looks like:
    INQUIRY_PARTIAL 20:12:11:22:5d:4f 240404 "" -11
    INQUIRY 20:12:11:22:5d:4f 240404
    NAME 20:12:11:22:5d:4f "BoomBoom 560"
    I will program it in the if-else style to analyse the command and parse the parameters in a second step.
    @SGaist : Thanks for the link to the QRegularExpression Example. This is a great help.

Log in to reply