2

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

László Papp
  • 51,870
  • 39
  • 111
  • 135
y2k
  • 65,388
  • 27
  • 61
  • 86

1 Answers1

7

.*? 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"

László Papp
  • 51,870
  • 39
  • 111
  • 135
  • I had the same issue as @y2k, and after searching for a while I came across this answer. Not understanding why .*? was invalid for QT I did a follow up search and found out that .*? is only currently invalid because QT doesn't support non-greedy expressions: http://qt-project.org/wiki/Regexp_engine_in_Qt5 – Niko Jan 09 '15 at 20:09
  • Just to add that Qt supports non-greedy expressions, but not in the regexp code. See QRegExp::setMinimal. – George Y. May 09 '15 at 07:22