PDA

View Full Version : extract contents in double quote from a qstring



prabhudev
21st February 2013, 08:05
I have a string like

Abcd(efhj:xyz; abc:” hello”; pqr:“hi”)

how do i extract only hello and hi from the above string
please guide
thanks

alrawab
21st February 2013, 08:13
http://www.developer.nokia.com/Community/Wiki/Split_and_extract_strings_using_QString
just change delimiter Pattern

Lykurg
21st February 2013, 08:20
Use QRegularExpression to match quoted words. This way you get to captured text which would be hello and hi.

prabhudev
21st February 2013, 09:25
thanks both of you..its still not working
changing the delimiterpattern to

QString delimiterPattern("” ”")
and

QString delimiterPattern(" " " ")

didn't work

Lykurg
21st February 2013, 10:47
Of course not. Do not just copy and past. Try to understand what the code is actually doing. Instead of split I would focus on QRegularExpression.

wysota
21st February 2013, 12:08
Or QRegExp in Qt4.

alrawab
24th February 2013, 17:29
#include <QCoreApplication>
#include <QtCore>

int main(int argc, char *argv[])
{
//case double qoute delimeter
QString test("\"apple\" \"orange\" \"fig\"");
QStringList splits=test.split("\" \"",QString::SkipEmptyParts);
foreach(QString s,splits)
qDebug()<< s;

//using Regexp
QString v=" Abcd(efhj:xyz; abc:\" hello\"; pqr:\"hi\")";
QRegExp rx("\"([^\"]*)\"");
QStringList list;
int pos = 0;
while ((pos = rx.indexIn(v, pos)) != -1)
{
list << rx.cap(0);
pos += rx.matchedLength();
}
//qDebug()<<list;
foreach(QString str,list)
qDebug()<<str;


QCoreApplication a(argc, argv);

return a.exec();
}