felice.murolo
felice.murolo

Reputation: 166

QRegExp and colon after string

I have a problem with QRegExp. This is my source. I would like both the "Re:" and "Fwd:" substrings to be deleted:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QRegExp>
#include <iostream>

using namespace std;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QString s = "Fwd: Re: my subject line";
    cout << "original string: " << s.toLatin1().data() << endl;

    QRegExp rx("\\b(Re:|Fwd:)\\b");
    rx.setCaseSensitivity(Qt::CaseInsensitive);

    s.replace(rx,"");
    cout << "replaced string: " << s.toLatin1().data() << endl;

}

It doesn't works. The output:

original string: Fwd: Re: my subject line
replaced string: Fwd: Re: my subject line

If I remove the ':' character in the RegExp, the substrings "Re" and "Fwd" will be deleted, but ':' character remains into text.

How I an set the regexp expression to delete both "Re:" and "Fwd:" substrings from text?

Upvotes: 0

Views: 295

Answers (2)

felice.murolo
felice.murolo

Reputation: 166

I've found a solution.

QString s = "Fwd: Re: Re: my subject: line";
cout << "original string: " << s.toLatin1().data() << endl;

QRegExp rx("\\b(Re|Fwd)\\b[:]");
rx.setCaseSensitivity(Qt::CaseInsensitive);

s.replace(rx,"");
s = s.trimmed();
cout << "replaced string: " << s.toLatin1().data() << endl;

and the output is:

original string: Fwd: Re: Re: my subject: line
replaced string: my subject: line

Upvotes: 0

Lui Armstrong
Lui Armstrong

Reputation: 141

QRegExp rx("\\b(Re:|Fwd:)\\b");

\b only works with \w type, so you can actually write

QRegExp rx("\\b(Re:|Fwd:)");

or

QRegExp rx("(Re:|Fwd:)");

Upvotes: 2

Related Questions