[solved]extract values between double quotes in qstring
-
wrote on 21 Feb 2013, 08:04 last edited by
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 -
wrote on 21 Feb 2013, 08:11 last edited by
I'd parse the string manually. I notice you do have different type of double quotes, so manual parsing would most likely be easier. Also, do you want the hello with or without the spacebar?
-
wrote on 21 Feb 2013, 08:25 last edited by
You might want to use the QString::split('"') function. You get a QStringList with part 2 and 4 beeing your desired strings.
Greetz -
wrote on 21 Feb 2013, 08:32 last edited by
First, there are different double quotes, second, even if you split the string in a few stages with a few splitters, once the splitters are gone there is no way to tell whether a string fragment was in quotes.
-
You might be interested by using RegExps
-
wrote on 21 Feb 2013, 08:45 last edited by
As SGaist says "RegExp":http://qt-project.org/doc/qt-4.8/qregexp.html would be a good option.
-
wrote on 21 Feb 2013, 08:45 last edited by
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.
-
wrote on 21 Feb 2013, 08:52 last edited by
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);@
-
wrote on 21 Feb 2013, 09:20 last edited by
Won't work for his particular task. He uses extended characters, plus this method returns a single string, not all in quotes.
-
wrote on 21 Feb 2013, 09:31 last edited by
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)
-
wrote on 21 Feb 2013, 10:02 last edited by
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 -
wrote on 21 Feb 2013, 10:14 last edited by
"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);
...
@ -
wrote on 21 Feb 2013, 10:19 last edited by
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.
-
wrote on 21 Feb 2013, 10:32 last edited by
[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.
-
wrote on 21 Feb 2013, 10:43 last edited by
[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.
-
wrote on 21 Feb 2013, 10:49 last edited by
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 ;)
-
wrote on 21 Feb 2013, 11:04 last edited by
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]
7/18