goGud
goGud

Reputation: 4333

How to get rid of parentheses and theirs content in a QString?

I am trying to get rid of parentheses and the words contained in these parentheses in a QString. Could you please help me?

var1.at(0) = " MainWindow(QWidget *parent) : ";

QStringList var2 = var1.at(0).split(QRegExp("\("), QString::SkipEmptyParts);

For this example, I am trying to get the function name which is only : MainWindow

I couldn't solve it with :

QRegExp("(");
QRegExp("\(");
QRegExp("\\(");
QRegExp("\((");

Upvotes: 0

Views: 249

Answers (1)

AnatolyS
AnatolyS

Reputation: 4319

To get function name you can use the following, if your function name is the first identifier in the string:

QString s("  MainWindow(QWidget *parent) : ");
QRegExp e("(\\w+)");
if( e.indexIn(s) != -1 ) {
    qDebug() << e.cap(1); // MainWindow
}

If you want to get all until '(', you can use:

QString s("  MainWindow(QWidget *parent) : ");
QRegExp e("([^(]+)");
if( e.indexIn(s) != -1 ) {
    qDebug() << e.cap(1).trim();
}

Upvotes: 1

Related Questions