我有QRegExp的问题。 这是我的来源。我想要" Re:"和" Fwd:"要删除的子字符串:
#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;
}
它不起作用。输出:
original string: Fwd: Re: my subject line
replaced string: Fwd: Re: my subject line
如果我删除&#34;:&#34; regexp中的char,substring&#34; Re&#34;和&#34; Fwd&#34;将被删除,但&#34;:&#34; char留在文本中。
如何设置regexp表达式以删除&#34; Re:&#34;和&#34; Fwd:&#34;来自文本的子串?
问候。
答案 0 :(得分:1)
QRegExp rx("\\b(Re:|Fwd:)\\b");
\ b仅适用于\ w类型(如果我错了,请纠正我),所以你实际上可以写
QRegExp rx("\\b(Re:|Fwd:)");
或
QRegExp rx("(Re:|Fwd:)");
答案 1 :(得分:0)
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
Someone have a better way?
Regards