Hello,
I have problem with negative regexp in QT,
I've want capture string after "//"
example: in "//aaa" - i want "aaa"
but I dont want capture when before "//" is http: or https:
I've tried this 4 regexp, but all are wrong ...
please help
#include <QCoreApplication>
#include <QRegExp>
#include <QDebug>
int main(int argc, char *argv[])
{
QRegExp rx1
("(ftp:|http:|https:){0}//([a-zA-Z0-9_-]+)");
QRegExp rx2
("[^(ftp:|http:|https:)]//([a-zA-Z0-9_-]+)");
QRegExp rx3
("(?=(ftp:|http:|https:))//([a-zA-Z0-9_-]+)");
QRegExp rx4
("(?!(ftp:|http:|https:))//([a-zA-Z0-9_-]+)");
strData = "//aaa";
// should be != -1
qDebug() << "1:" << rx1.indexIn(strData) << " " << (rx1.indexIn(strData) != -1 ? "OK" : "WRONG");
qDebug() << "2:" << rx2.indexIn(strData) << " " << (rx2.indexIn(strData) != -1 ? "OK" : "WRONG");
qDebug() << "3:" << rx3.indexIn(strData) << " " << (rx3.indexIn(strData) != -1 ? "OK" : "WRONG");
qDebug() << "4:" << rx4.indexIn(strData) << " " << (rx4.indexIn(strData) != -1 ? "OK" : "WRONG");
qDebug() << "";
strData = "http://aaa";
// should be == -1
qDebug() << "1:" << rx1.indexIn(strData) << " " << (rx1.indexIn(strData) == -1 ? "OK" : "WRONG");
qDebug() << "2:" << rx2.indexIn(strData) << " " << (rx2.indexIn(strData) == -1 ? "OK" : "WRONG");
qDebug() << "3:" << rx3.indexIn(strData) << " " << (rx3.indexIn(strData) == -1 ? "OK" : "WRONG");
qDebug() << "4:" << rx4.indexIn(strData) << " " << (rx4.indexIn(strData) == -1 ? "OK" : "WRONG");
// rx.cap ... etc
return a.exec();
}
#include <QCoreApplication>
#include <QRegExp>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QRegExp rx1("(ftp:|http:|https:){0}//([a-zA-Z0-9_-]+)");
QRegExp rx2("[^(ftp:|http:|https:)]//([a-zA-Z0-9_-]+)");
QRegExp rx3("(?=(ftp:|http:|https:))//([a-zA-Z0-9_-]+)");
QRegExp rx4("(?!(ftp:|http:|https:))//([a-zA-Z0-9_-]+)");
QString strData;
strData = "//aaa";
// should be != -1
qDebug() << "1:" << rx1.indexIn(strData) << " " << (rx1.indexIn(strData) != -1 ? "OK" : "WRONG");
qDebug() << "2:" << rx2.indexIn(strData) << " " << (rx2.indexIn(strData) != -1 ? "OK" : "WRONG");
qDebug() << "3:" << rx3.indexIn(strData) << " " << (rx3.indexIn(strData) != -1 ? "OK" : "WRONG");
qDebug() << "4:" << rx4.indexIn(strData) << " " << (rx4.indexIn(strData) != -1 ? "OK" : "WRONG");
qDebug() << "";
strData = "http://aaa";
// should be == -1
qDebug() << "1:" << rx1.indexIn(strData) << " " << (rx1.indexIn(strData) == -1 ? "OK" : "WRONG");
qDebug() << "2:" << rx2.indexIn(strData) << " " << (rx2.indexIn(strData) == -1 ? "OK" : "WRONG");
qDebug() << "3:" << rx3.indexIn(strData) << " " << (rx3.indexIn(strData) == -1 ? "OK" : "WRONG");
qDebug() << "4:" << rx4.indexIn(strData) << " " << (rx4.indexIn(strData) == -1 ? "OK" : "WRONG");
// rx.cap ... etc
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks