Cannot find key in INI file with QRegExp
-
Hi friends
I have the simple INI file. Here some content:
@
<ini>
[ right section too ] // I can found it with section(QString("^\s*\[\s*%1\s*\]").arg("right section too"));
data=123 //I try to extract value '123' with keyLine(QString("^\s*%1\s*=\s*(.)\s((//)|#.*)?").arg("data"))
word = word # bla bla bla
</ini>
@
Now code's pieces:@
QRegExp section(QString("^\s*\[\s*%1\s*\]").arg("right section too"));
QREgexp keyLine(QString("^\s*%1\s*=\s*(.)\s((//)|#.*)?").arg("data"));QStringList fileContent = getFileContent(iniFileName);
QStringList::iterator item = fileContent.begin();
QStringList::iterator atEnd = fileContent.end();while(item != atEnd)
{
if(section.indexIn(*item) //here we found the section
{
++item; //pass section goto next line
while(item != atEnd) //I know you'll lose next sections
{
if(keyLine.indexIn(*item)
{
printf("%s\n", item->toAscii().data());
for(int i = 1; i < keyLine.captureCount(); ++i)
printf("%d: '%s'\n", i, keyLine.cap(i));
}
++item;
}
}
++item;
}
@I was wander when I seen
@
word = word # bla bla bla
1: ''
2: ''
@When I change keyLine to
@
keyLine(QString("^\s*%1\s*=\s*(.)\s((//)|#.*)?\$").arg("data"));
@
I seen@data=123 //I try to extract value '123' with keyLine(QString("^\s*%1\s*=\s*(.)\s((//)|#.)?").arg("data"))
1: ''
2: ''
@
When I feed egrep with
@
"^\sdata\s*=\s*(.)\s((//)|#.*)?"
@
it found all line entriesSo Q: where I wrong?
Regards
PS
My real code is differ I show but the idea is sameEdit: Please use @ tags around code sections; Andre
-
When I change keyLine from
@
(QString("^\s*%1\s*=\s*(.)\s((//)|#.)?").arg("data"))
@
to
@
(QString("^\s%1\s*=\s*(.\)\s((//)|#.\*)?").arg("data"))
@
I seendata=123 //I try to extract value...
but capturedTexts().at(1).toAscii().data() == ''
So how to get found value??
-
Yehh
I divide query onto 2 parts
@
QString CInifile::_getValue(const QString & section, const QString &key)
{
QStringList::const_iterator item = _getSectionItem(section);
QStringList::const_iterator atEnd = _inifileContent.constEnd();
if(item == atEnd)
return QString::null;QRegExp rxWithComment(QString("^\s*%1\s*=(\s*)(.+)(?=\s)(\s+)(//)|#.").arg(key));
QRegExp rxWithoutComment(QString("^\s%1\s*=(\s*)(.+)(?=\s)(\s+)").arg(key));
QRegExp *rx;while(++item != atEnd)
{
rx = &rxWithoutComment;
if((item->indexOf("//") >= 0) || (item->indexOf("//") >= 0))
rx = &rxWithComment;if(rx->indexIn(*item) > 0)
return rx->cap(2);
}
return QString::null;
}
@Question to Andre
Some time ago I worked with the MySql NDB cluster. In my.ini there were N [mysqld] sections where N equals number of MySql servers in the cluster. So in which section of N QSettings will store a value, 4Ex,
@
QSettings::setValue("mysqld/port", 1234);
@
?