y2k
y2k

Reputation: 66016

C++ Qt - QString remove() regex between {brackets}

I have tried the following regular expressions to remove {anything} between brackets (and hopefully the brackets themselves)!

    mystr.remove(QRegExp("\\{(.*?)\\}"));
    mystr.remove(QRegExp("\{(.*?)\}"));

Nothing is removed

Upvotes: 2

Views: 4078

Answers (1)

László Papp
László Papp

Reputation: 53205

.*? is invalid. Try the following code:

main.cpp

#include <QString>
#include <QDebug>
#include <QRegExp>

int main()
{
    QString mystr = "te{foo}st";
    qDebug() << mystr.remove(QRegExp("\\{(.*)\\}"));

    return 0;
}

Compilation

This may not be the exact command you need to run, so try to adjust the concept for your particular scenario.

g++ -I/usr/include/qt/QtCore -I/usr/include/qt -fPIC -lQt5Core main.cpp && ./a.out

Output: "test"

Upvotes: 7

Related Questions