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() Which QRegExp to split parameters?
Forum Update on Monday, May 27th 2025

QString.split() Which QRegExp to split parameters?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 3.6k Views
  • 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.
  • _ Offline
    _ Offline
    _M_H_
    wrote on 14 Oct 2016, 07:55 last edited by
    #1

    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 = parameter.at(0);	//stringCmd == NAME
    stringId = parameter.at(1);	//stringId == 00:12:34:56:78:90
    stringName = parameter.at(2);	//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?
    Regards,
    Michael

    R 1 Reply Last reply 14 Oct 2016, 08:03
    0
    • _ _M_H_
      14 Oct 2016, 07:55

      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 = parameter.at(0);	//stringCmd == NAME
      stringId = parameter.at(1);	//stringId == 00:12:34:56:78:90
      stringName = parameter.at(2);	//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?
      Regards,
      Michael

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 14 Oct 2016, 08:03 last edited by
      #2

      @_M_H_
      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);
      }
      

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      2
      • _ Offline
        _ Offline
        _M_H_
        wrote on 14 Oct 2016, 08:31 last edited by _M_H_
        #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?
        Michael

        R 1 Reply Last reply 15 Oct 2016, 16:12
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 14 Oct 2016, 21:18 last edited by
          #4

          Hi,

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          2
          • _ _M_H_
            14 Oct 2016, 08:31

            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?
            Michael

            R Offline
            R Offline
            raven-worx
            Moderators
            wrote on 15 Oct 2016, 16:12 last edited by
            #5

            @_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
            }
            

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            1
            • _ Offline
              _ Offline
              _M_H_
              wrote on 17 Oct 2016, 06:27 last edited by
              #6

              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 1
              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.

              1 Reply Last reply
              2

              1/6

              14 Oct 2016, 07:55

              • Login

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