[solved]extract values between double quotes in qstring
-
You might want to use the QString::split('"') function. You get a QStringList with part 2 and 4 beeing your desired strings.
Greetz -
You might be interested by using RegExps
-
As SGaist says "RegExp":http://qt-project.org/doc/qt-4.8/qregexp.html would be a good option.
-
Learning regular expressions will be considerably more challenging and time consuming than writing a simple parsing loop like this one:
@QString str("Abcd(efhj:xyz; abc:” hello”; pqr:“hi”)");
QStringList list;
bool isValid = 0;
int s = 0, f = 0;for (int i = 0; i < str.length(); ++i) {
if (str[i] == 8220 || str[i] == 8221 || str[i] == 34) {
if (isValid) {
f = i;
list.append(str.mid(s + 1, f - s - 1));
isValid = 0;
}
else {
s = i;
isValid = 1;
}
}
}for (int i = 0; i < list.length(); ++i ) qDebug() << list.at(i);@
If could be done more sophisticated by tracking for opening and closing quotes as well as depth in case of nesting.
That doesn't mean learning regular expressions will not be beneficial, especially in the long run.
-
Ok.If RegExp hard for first time try this:
@
QString MainWindow::getDataBetween(QString begin,QString end, QString &source)
{
int startIndex = source.indexOf(begin)+begin.length();
if(startIndex <= 0)return QString();
int endIndex = source.indexOf(end,startIndex);
if(endIndex <= 0)return QString();
return source.mid(startIndex,endIndex - startIndex);
}@then call it like:
@QString str1 = getDataBetween(":"",""",sourceString);@
-
Whilst reg exp's are a complex subject, they really are worth learning and should be a weapon in every developers arsenal. Therefore, I feel honor bound to provide the reg exp solution for this as follows:
@
#include <QtCore>
#include <QDebug>int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);QRegExp re("\"([A-Za-z0-9_\\./\\-\\s]*)\""); QString str="Abcd(efhj:xyz; abc:\\" hello\"; pqr:\"hi\")"; QStringList list; int pos=0; while((pos=re.indexIn(str, pos))!=-1){ list << re.cap(1); pos+=re.matchedLength(); } qDebug() << "LIST:" << list;
}
@Which results in:
LIST: (" hello", "hi")
Hope this helps ;o)
-
thanks all
my qstring itself as double quotes means
wen i use qDebug it shows
"Abcd(efhj:xyz; abc:” hello”; pqr:“hi”)"
so what ever i do i am ending up with whole string again.
How to get rid of double quotes from whole string so that i can easily fetch hi and hello -
"QString":http://qt-project.org/doc/qt-4.8/qstring.html provides a lot of functionality for string manipulation, the following is probably the simplest:
@
...
if(myString.startsWith(""") myString.remove(0,1);
if(myString.endsWith(""") myString.remove(myString.size()-1,1);
...
@ -
qDebug() shows "" for strings, but that doesn't mean the quotes are in the string, it is just decoration to signify it is a string.
The example I posted above will do exactly what you want with the input you specified.
@qxoz - " is different from ” is different from ” - " is ASCII 34, his string has extended ASCII characters that are equivalent to 8220 and 8221
Also, your solution won't apply to an arbitrary string, only to the one given as an example and relies on knowing what the string is exactly in advance. The solution needs to be more generic and able to work both on the example string as well as on some other.
-
[quote author="jazzycamel" date="1361439115"]Whilst reg exp's are a complex subject, they really are worth learning and should be a weapon in every developers arsenal. Therefore, I feel honor bound to provide the reg exp solution for this as follows[/quote]
No argument here - the regex solution is the fastest to implement, but it is also complete magic to a newbie. It involves what appears to be a magical string and mysterious methods. While it gets the job done, it is not very educational.
-
[quote author="utcenter" date="1361442748"]No argument here - the regex solution is the fastest to implement, but it is also complete magic to a newbie. It involves what appears to be a magical string and mysterious methods. While it gets the job done, it is not very educational.[/quote]
The problem is regexp's (like anything) will remain "magic" until you take the time to understand them.
And I'm not sure about not being very educational: IMHO, the problem here is simple and well bounded with defined input and output and a clear, open solution. Along with the abundance of documentation available for "QRegExp":http://qt-project.org/doc/qt-4.8/qregexp.html and regexp's in general, this is an ideal opportunity to learn about a very powerful technique early on and not get into the habit of coding around it.
-
As I said earlier - it is indeed beneficial to learn regular expressions. But so is building up a concept of how to parse text manually, which is a subject much better suited for a beginner. And while both manual parsing and regex can solve this problem, the two approaches are significantly different in their general application. Sorry if I sounded like I was downplaying your solution - it wasn't my intent ;)
-
Worked!!!!!Thanks
[quote author="jazzycamel" date="1361439115"]Whilst reg exp's are a complex subject, they really are worth learning and should be a weapon in every developers arsenal. Therefore, I feel honor bound to provide the reg exp solution for this as follows:@
#include <QtCore>
#include <QDebug>int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);QRegExp re("\"([A-Za-z0-9_\\./\\-\\s]*)\""); QString str="Abcd(efhj:xyz; abc:\\\\" hello\"; pqr:\"hi\")"; QStringList list; int pos=0; while((pos=re.indexIn(str, pos))!=-1){ list << re.cap(1); pos+=re.matchedLength(); } qDebug() << "LIST:" << list;
}
@Which results in:
LIST: (" hello", "hi")
Hope this helps ;o)[/quote]