Nominate our 2022 Qt Champions!

[SOLVED] QRegExp help needed

  • 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];
            emit display("Got bad message from client:" + client->peerAddress().toString() + line);

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

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

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

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

Log in to reply