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. [SOLVED] QRegExp help needed
Forum Update on Tuesday, May 27th 2025

[SOLVED] QRegExp help needed

Scheduled Pinned Locked Moved General and Desktop
5 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.
  • K Offline
    K Offline
    kalster
    wrote on 3 Dec 2011, 05:31 last edited by
    #1

    I am having problems with the QRegExp in my client / server app. the following is what i currently have and it works good.
    @// Normal messges look like this: "username:The message"
    QRegExp messageRegex("^([^:]+):(.*)$");
    QString user = messageRegex.cap(1);
    QString message = messageRegex.cap(2);@

    in the above code, it brakes up the "username", and "The message" into two variables. the problem is that i need to add another variable to the list for the private chat. i would like it to be the following but it is not working. i am getting a bad message error.

    @// Normal messges look like this: "username:tabName#The message"
    QRegExp messageRegex("^([^:]+):([^#]+)#(.*)$");@

    in the client, I am trying to brake "username", "tabName" and "The message" into three different variables.

    in the following code it reads from the client. if the message = -1 then give the error...

        @QString line = QString::fromUtf8(client->readLine()).trimmed();
        QRegExp meRegex("^/me:(.*)$");
        if(meRegex.indexIn(line) != -1)
        {
            user = meRegex.cap(1);
            users[client] = user;
        }
        else if(users.contains(client))
        {
            QString message = line;
            user = users[client];
        }
        else
        {
            emit display("Got bad message from client:" + client->peerAddress().toString() + line);
        }
    }@
    
    1 Reply Last reply
    0
    • V Offline
      V Offline
      veeeee_d
      wrote on 3 Dec 2011, 09:46 last edited by
      #2

      Don't underestimate the power of a simple, real parser over regular expressions. These are often a very good way of getting things done and done even quicker than with RegExes.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kalster
        wrote on 3 Dec 2011, 09:51 last edited by
        #3

        can i have a link to the parser docs please :)

        1 Reply Last reply
        0
        • V Offline
          V Offline
          veeeee_d
          wrote on 3 Dec 2011, 11:15 last edited by
          #4

          Not a docs... Just use regular QString functions for finding ":" and sub-stringing.

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kalster
            wrote on 4 Dec 2011, 07:12 last edited by
            #5

            thank you veeeee_d. i have also decided later after your first post that QString functions are the best for what i need to do. :). this topic is solved.

            1 Reply Last reply
            0

            1/5

            3 Dec 2011, 05:31

            • Login

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